我必须查看控制器..我想从第一个视图传递数据到第二个视图我有这个代码
用于ViewController.h
#import <UIKit/UIKit.h>
#import "SecondView.h"
@interface ViewController : UIViewController{
SecondView *secondviewData;
IBOutlet UITextField *textfield;
}
@property (nonatomic,retain) SecondView *secondviewData;
-(IBAction)passdata:(id)sender;
@end
用于viewController.m
#import "ViewController.h"
#import "SecondView.h"
@interface ViewController ()
@end
@implementation ViewController
@synthesize secondviewData;
-(IBAction)passdata:(id)sender{
SecondView *second= [[SecondView alloc] initWithNibName:nil bundle:nil];
self.secondviewData=second;
secondviewData.passedValue=textfield.text;
[self presentModalViewController:second animated:YES];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
用于第二个视图控制器,即SecondView.h
#import <UIKit/UIKit.h>
@interface SecondView : UIViewController{
IBOutlet UILabel *label;
NSString *passedValue;
}
@property (nonatomic,retain) NSString *passedValue;
-(IBAction)back:(id)sender;
@end
用于SecondView.m
#import "SecondView.h"
#import "ViewController.h"
@interface SecondView ()
@end
@implementation SecondView
@synthesize passedValue;
-(IBAction)back:(id)sender{
ViewController *vc= [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:vc animated:YES];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
label.text=passedValue;
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
我遇到的错误是nowModalViewController在这两行中弃用了iOS 6
[self presentModalViewController:vc animated:YES];
[self presentModalViewController:vc animated:YES];
当我运行它并点击按钮时它将停止工作并显示一个空白的黑页
答案 0 :(得分:3)
您可以在AppDelegate
中保存数据,以便跨应用程序中的视图控制器访问它。您所要做的就是创建AppDelegate
AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
答案 1 :(得分:0)
如果您要为&gt; = iOS 5构建,那么您应该使用presentViewController:animated:completion:
而不是presentModalViewController:animated:
。
此外,&#39;回归&#39;并不意味着推新事物。所以,这段代码:
- (IBAction)back:(id)sender {
ViewController *vc= [[ViewController alloc] initWithNibName:nil bundle:nil];
[self presentModalViewController:vc animated:YES];
}
应改为:
- (IBAction)back:(id)sender {
[self dismissViewControllerAnimated:YES completion:nil];
}
现在,您应该看一下使用委托模式将数据传递回原始视图控制器。并且优选地,该委托实现将包括告知代理第二个控制器已完成并且委托触发解雇。