请教一个多场景传值问题 楼主你好,控制器之间的传值有很多种方法,可以试试通知或者代理或者Block 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 先检查 self.presentingViewController 是否为nil. 好像self.presentingViewController 不能获取到presenting的控制器实例,如果获取不到那么返回值就会是nil.你后面取emailAddress就没有什么意义像这种 A->B 之间的传值使用属性传递就可以了。在B控制器中定义一个属性来接收A传递过来的值//////B中声明一个属性@property (nonatomic,strong) NString *email;///////A中在弹出B时将label中显示的email传递给BB *b = [B new];b.email=self.emailAddress.text;[A presentViewController:b animated:YES completion:nil];//////B中在viewDidLoad中将A传递过来的值赋给文本框- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.emailAddress.text=self.email; UIAlertView *alertView; alertView=[[UIAlertView alloc] initWithTitle:self.email message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil]; [alertView show];} 如果是 A 作用(push or present)出来 B, 想将B的值回传给A .那么你可以使用的传递方式可以用多种,使用代理(delegate), block, 再或者NSNotificationCenter 提一下,我使用的是IB来创建视图,也就是说,照楼上的方法,UI得用代码写,那么现在如果不用代码写UI,我是不能像楼上那样直接创建控制器实例的,这种情况下要么解决一下presentingViewContoller是nil的问题,要么寻找另一种解决方法? 我找到一个问题了。我发现,确实presentingViewController这个方法返回值确实是nil,但只是在viewDidLoad里面是nil。首先,有A,B场景,我在B场景里面的viewDidLoad里面去掉 self.EmailAddress.text=((ViewController *)self.presentingViewController).emailAddress.text;然后在视图里加了个按钮,实现如下:- (IBAction)showEmail:(id)sender { self.EmailAddress.text=((ViewController *)self.presentingViewController).emailAddress.text;}启动程序,点击按钮以后成功了: 自己探索解决了,解决方法是代码:如果是用IB写故事版可以在A场景Controller.m添加如下代码:-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{ if ([segue.identifier isEqualToString:@"toEditor"]) { id thesegue=segue.destinationViewController; //[thesegue setValue:self.emailAddress.text forKey:@"email"]; EditorViewController *editor=(EditorViewController *)thesegue; editor.email=self.emailAddress.text; }}在B场景Controller.m的ViewDidLoad添加如下代码:- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. NSLog(@"传过来的值:%@",_email); self.EmailAddress.text=self.email; }注意,在B场景.h文件里添加公有属性email;感谢楼上的楼上的楼上为我解答。 请教【弹出对话框问题】 请问如何实现UITableViewCell异步下载图片并根据图片大小自动调整Cell的高度 函数内实例化并返回的对象如何释放? 高德地图反向解析的地址怎么是英文的!怎么转中文地址? SWIFT文件路径的问题 关于APP图标问题 虚心请教Mac购买建议 现有ipa文件,需要修改里面的一个域名 xcode8 复制粘贴截切快捷键不能用 ios js怎么将后台生成的token保存到cookie xcode6 empty application SDWebImage在64位架构下报错,求大神指点!
像这种 A->B 之间的传值使用属性传递就可以了。在B控制器中定义一个属性来接收A传递过来的值
//////B中声明一个属性
@property (nonatomic,strong) NString *email;///////A中在弹出B时将label中显示的email传递给B
B *b = [B new];
b.email=self.emailAddress.text;
[A presentViewController:b animated:YES completion:nil];//////B中在viewDidLoad中将A传递过来的值赋给文本框
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.emailAddress.text=self.email;
UIAlertView *alertView;
alertView=[[UIAlertView alloc] initWithTitle:self.email message:nil delegate:nil cancelButtonTitle:@"ok" otherButtonTitles:nil, nil];
[alertView show];
}
我发现,确实presentingViewController这个方法返回值确实是nil,但只是在viewDidLoad里面是nil。
首先,有A,B场景,我在B场景里面的viewDidLoad里面去掉 self.EmailAddress.text=((ViewController *)self.presentingViewController).emailAddress.text;然后在视图里加了个按钮,实现如下:- (IBAction)showEmail:(id)sender {
self.EmailAddress.text=((ViewController *)self.presentingViewController).emailAddress.text;
}启动程序,点击按钮以后成功了:
如果是用IB写故事版
可以在A场景Controller.m添加如下代码:-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:@"toEditor"]) {
id thesegue=segue.destinationViewController;
//[thesegue setValue:self.emailAddress.text forKey:@"email"];
EditorViewController *editor=(EditorViewController *)thesegue;
editor.email=self.emailAddress.text;
}
}在B场景Controller.m的ViewDidLoad添加如下代码:- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(@"传过来的值:%@",_email);
self.EmailAddress.text=self.email;
}注意,在B场景.h文件里添加公有属性email;
感谢楼上的楼上的楼上为我解答。