Ios 从一页到另一页提取数据
我知道这个问题以前已经得到了回答,但我在其中遇到了困难。在一个页面上有一个文本字段,我想通过单击按钮将其内容检索到位于下一页的标签中 我试过这个。tfName是第1页中文本字段的idIos 从一页到另一页提取数据,ios,objective-c,Ios,Objective C,我知道这个问题以前已经得到了回答,但我在其中遇到了困难。在一个页面上有一个文本字段,我想通过单击按钮将其内容检索到位于下一页的标签中 我试过这个。tfName是第1页中文本字段的id -(IBAction)onClick:(id)sender { NSString *string1 =tfName.text; } 在第2页 - (void)viewDidLoad { [super viewDidLoad]; label1.text=string1; } 首先创建ViewContr
-(IBAction)onClick:(id)sender
{
NSString *string1 =tfName.text;
}
在第2页
- (void)viewDidLoad {
[super viewDidLoad];
label1.text=string1;
}
首先创建ViewController对象,然后为该属性赋值
-(IBAction)onClick:(id)sender
{
SecondViewController *tempView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
tempView. string1 = tfName.text;
[self.navigationController pushViewController:tempView animated:YES];
}
在SecondViewController.h
上创建一个全局字符串,如
@Property(nonatomic, Strong)NSString *string1;
@Synthesize string1;
在您的实现中SecondViewController.m
像
@Property(nonatomic, Strong)NSString *string1;
@Synthesize string1;
终于打电话来了
- (void)viewDidLoad {
[super viewDidLoad];
if (string1.length >0)
label1.text=string1;
}
下面是简单的示例代码: A.h类:
UITextfield *tfName;
上午一班:
tfName.text = @"Hello world";
-(IBAction)onClick:(id)sender
{
ClassB *b = [[ClassB alloc]initwithnibname@"ClassB"];
b.string1 =tfName.text;
[self.navigationController pushViewController:b animated:YES];
}
B.h类:
@Property(..)NSString *string1;
B.m类:
label1.text = _string1.
注意:您需要在ClassA中导入ClassB,即导入“ClassB.h”
希望这能有所帮助。做一个单身班
class DataModel {
class var sharedInstance: DataModel {
get {
struct Static {
static var instance: DataModel? = nil
static var token: dispatch_once_t = 0
}
dispatch_once(&Static.token, {
Static.instance = DataModel()
})
return Static.instance!
}
}
var textData:String = ""
}
点击按钮,执行以下操作:
DataModel.sharedInstance.textData = "page 1 data"
在第2页查看下载基金
label1.text = DataModel.sharedInstance.textData
那应该行得通 您是否使用了故事板??您是否使用了故事板,或者请将导航或移动到您的secondviewcontroller逻辑。如何在pop view controller中传递数据,是否可以直接传递…抱歉,它的pushviewcontroller不是PopViewController您在哪里创建了string1变量?它是否在此页面或调用页面中。在SecondViewController中创建@Property(非原子,强)NSString*string1;property@KhyatiVaishnav--检查更新的答案,它可以工作