请教一个多场景传值问题 楼主你好,控制器之间的传值有很多种方法,可以试试通知或者代理或者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;感谢楼上的楼上的楼上为我解答。 iphone有没有api可以自动配置vpn的ip/用户名/密码的信息,然后自动连接到该vpn的办法? xcode4使用静态库的问题 ipad 硬件解码 播放m3u8文件时,碰到一个有问题的TS文件后,播放停止,有办法自动跳过继续播放吗? 调试一段tableview利用coredata进行分组和插入数据的代码 短信分享怎么实现?自己写的会出现闪退 uiwebview打开网页崩溃。求大神。 二维码扫描软件 NSTimer的释放问题 用什么语言可以很好的移植一个android平台的APP倒IOS 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;
感谢楼上的楼上的楼上为我解答。