目的:在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/