从一个ViewController到另一个Webview的HTML文本

时间:2016-11-02 15:59:17

标签: ios objective-c webview uiviewcontroller uiwebview

目的:在viewcontroller1的文本字段按下按钮中写一些html代码,结果显示在viewcontroller2的webview中 这就是我到目前为止所写的内容

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

    @interface vc1 : UIViewController

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

文本字段可以只是标准字段,我需要做的就是确保 当用户点击按钮时,vc2出现在它的HTML部分 是我从文本字段中获取的字符串 负责此行动的方法如下所述

 -(IBAction) btnAction : (id) sender;

    @end

    @interface vc2 : UIViewController

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

    @end

//没有找到vc1的标准实现 //尝试实现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];

这是我应该告诉它从vc1获取文本并将其显示为HTML

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

某种东西,但字符串被替换为插入vc1文本字段的内容     }

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

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

@end

编辑:感谢您的发帖,这个社区非常有帮助。

2 个答案:

答案 0 :(得分:1)

vc2.h的界面中声明属性

@property (strong, nonatomic) NSString *htmlString;

vc1.h文件或vc1.h添加

#import "vc2.h"

现在在按钮操作中使用以下代码:

-(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];
 }

最后在vc1.m使用:

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

希望这会有所帮助。如有任何特定错误,请通知我。

答案 1 :(得分:1)

在Swift中

let secondViewController = self.storyboard?.instantiateViewControllerWithIdentifier("SecondViewController") as! SecondViewController

secondViewController.myStringValue = myTextField.text
self.navigationController?.pushViewController(secondViewController, animated: true)

将webview加载到secondViewController的viewDidload方法

相关问题