Spartak Shahbazyan Spartak Shahbazyan - 16 days ago 5
iOS Question

HTML text from one ViewController to another's Webview

the purpose: write some html code in viewcontroller1's text field press button, the result is shown in webview in viewcontroller2
This is what i've written so far

#import <UIKit/UIkit.h>
#import <Foundation/Foundation.h>

@interface vc1 : UIViewController

@property (weak, nonatomic) IBOutlet UIButton * btn;
@property (weak, nonatomic) IBOutlet UITextField * tf;


the text field can be just a standard one, all i need to do is make sure
that when the user hits the button the vc2 comes up and in it's HTML section
is a string that i got from the textfield
the method responsible for this action is mentioned below

-(IBAction) btnAction : (id) sender;

@end

@interface vc2 : UIViewController

@property (weak, nonatomic) IBOutlet UIWebView * wv;

@end


//didn't find standard implementation for vc1
//attempted implementation for vc1

@implementation vc1
@synthesize btn, tf;
-(id) initWithNibName: (NSString *)nibName0rNil bundle: (NSBundle *)nibBundle0rNil{
self = [super initWithNibName: nibName0rNil bundle: nibBundle0rNil];
if(self){
//custom init
}
return self;
}

-(IBAction) btnAction : (id) sender {
//initialize vc2? it's a class so i can't call it.
//can i save the contents of the object as a NSString? or can i just give the Webview a textfield object to show as HTML?


}

@end

@implementation vc2
@synthesize wv;

-(id) initWithNibName: (NSString *)nibName0rNil bundle: (NSBundle *)nibBundle0rNil{
self = [super initWithNibName: nibName0rNil bundle: nibBundle0rNil];
if(self){
//custom init
}
return self;
}

-(void)viewDidLoad{
[super viewDidLoad];


this is where i am supposed to tell it to take the text from vc1 and display it as HTML

[wv loadHTMLString:@"<html><body>YOUR-TEXT-HERE</body></html>" baseURL:nil]


something of the sort but the string is replaced with what was inserted into vc1's text field
}

-(void)viewDidUnload{
[self setWebview: nil];
[super viewDidUnload];
}

-(BOOL)shouldAutorotateToTinterfaceOrientation:(UIInterfaceOrientation)interfaveOrientation{
return interfaceOrientation == UIInterfaceOrientationPortrait;
}

@end


Edit: thank you for posting, this community has been very helpful.

Answer

In Interface of vc2.h declare a property like

@property (strong, nonatomic) NSString *htmlString;

In vc1.h file or vc1.h add

#import "vc2.h"

Now in the Button Action Use the following code:

-(IBAction) btnAction : (id) sender {
    //initialize vc2? it's a class so i can't call it.
    //can i save the contents of the object as a NSString? or can i just give the Webview a textfield object to show as HTML?
   //With Xib 
     vc2 *secondVC =[[vc2 alloc]initWithNibName:@"vc2" bundle:nil];
     secondVC.htmlString = self.tf.text;//textField property name
    [self.navigationController pushViewController:secondVC animated:YES];
 }

Finally to display the WebView in vc1.m use :

NSString *myHTMLString = [[NSString alloc] initWithFormat:@"%@", self.htmlString];
[self.wv loadHTMLString:myHTMLString baseURL:nil];

Hope this helps. Please let me know in case of any specific errors.

Comments