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

标签 ios objective-c webview uiviewcontroller uiwebview

目的:在viewcontroller1的text field press button中编写一些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

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

最佳答案

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

希望这可以帮助。如果有任何具体错误,请告诉我。

关于ios - 从一个 ViewController 到另一个 Webview 的 HTML 文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40384402/

相关文章:

ios - 在 Swift 中保存数据

ios - 地幔自动将 0 和 1 转换为 BOOL?

objective-c - 添加一个( double )变量到 NSMutableArray

android - 如何在 WebView 中使用自定义字体

ios - 自动布局 UICollectionView iOS 8

objective-c - NSDateFormatter 从字符串返回错误的日期

JavaFX WebView 弹出处理程序未尝试加载页面

Android Webview 在 4.4 上卡住,nativeOnDraw 失败;清除为背景颜色

ios - 在我的简单案例中,http 正文中的 JSON 数据包含特殊字符

ios - 使用钥匙串(keychain)存储设备标识符