在两个视图控制器之间传递数据

时间:2012-07-28 18:38:06

标签: ios objective-c iphone cocoa-touch uiviewcontroller

我有两个名为DataPass和ViewController的视图控制器。我想将数据从DataPass传递给ViewController。我在ViewController中有一个标签,我的ViewController中有一个UIButton,它会解散自己,而解雇会将数据传递给DataPass中的标签。

我做不到。请帮忙。这是我的代码:

ViewController.h

@interface ViewController : UIViewController
- (IBAction)sayfaGec:(id)sender;
@property (retain, nonatomic) IBOutlet UILabel *label;
+(ViewController *)hop;

ViewController.m

+(ViewController *)hop{
    static ViewController *myInstance = nil;
    if (myInstance == nil){
        myInstance = [[[self class]alloc]init];
        myInstance.label.text = @"test";
    }
    return myInstance;
}

DataPass.h

- (IBAction)sayfaKapat:(id)sender;

DataPass.m

- (IBAction)sayfaKapat:(id)sender {
    [ViewController hop].label.text = @"ddsg";
    [self dismissModalViewControllerAnimated:YES];
}

1 个答案:

答案 0 :(得分:0)

ViewController.h

- (void)setLabelData:(NSString:)aString;

ViewController.m

- (void)setLabelData:(NSString:)aString{
    [yourLabel setText:aString];
}

在您的DataPass中,您说:

ViewController *vContr = [[ViewController aloc]init];
[vContr setLabelData: @"asta la vista baby!"];
[self.view addSubview:vContr.view];