1

I have two view controllers called DataPass and ViewController. I want to pass a data from DataPass to ViewController. I have a label in ViewController and I have a UIButton in my ViewController which will dismiss itself and while dismissing will pass a data to label in DataPass.

I could not do it. Please help. Here is my code:

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];
}
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • What's the problem -- without more code it's hard to say why this doesn't work. Does the dismissing work, and just assigning the text to the label fail? Maybe the label isn't wired up correctly in IB? – ehope Jul 28 '12 at 18:42
  • @ErikH. Yes the problem is just assigning the text to the label fail. I am a newbie at IOS development. I never pass data between controllers. But all the connections between label,button and view controllers are correct. –  Jul 28 '12 at 18:44
  • possible duplicate of [Passing Data between View Controllers](http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers) –  Jul 28 '12 at 18:51
  • http://stackoverflow.com/questions/5210535/passing-data-between-view-controllers/17539906#17539906 check this link – Gajendra K Chauhan Jul 09 '13 at 04:29

1 Answers1

0
ViewController.h

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

ViewController.m

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

In your DataPass you say:

ViewController *vContr = [[ViewController aloc]init];
[vContr setLabelData: @"asta la vista baby!"];
[self.view addSubview:vContr.view];
John Smith
  • 2,012
  • 1
  • 21
  • 33
  • It should work: after you create ViewController object, you have access to all it's declared instance methods. check your outlet to the label, is it hooked properly in xib?. – John Smith Jul 28 '12 at 19:06
  • I am using storyboard. Not xib files. a –  Jul 28 '12 at 19:08
  • in your storyboard you should see the controller of your viewController. you must hook the outlet to your label, otherwise it will not work. – John Smith Jul 28 '12 at 19:09
  • by the way, are you creating the label programmatically? – John Smith Jul 28 '12 at 19:09
  • I created it dragging into the .h file by the left mouse button. –  Jul 28 '12 at 19:11
  • sounds like the outlet has been automatically hooked. ok. try this: declare a string in viewcontroller. in the method I have posted, alloc and set that string instead of setting the label. then in viewdidappear, set the label text by that string. – John Smith Jul 28 '12 at 19:18
  • Actually it was an example project. I will use this in another project and viewdidappear method won't work in there because i will pass a method from a popover window to the label. Can you make another solution? :) –  Jul 28 '12 at 19:22
  • I am at work. let me get home (30 mins) and find me in skype "diabolator2". I will help you then – John Smith Jul 28 '12 at 19:26
  • Ok. Thank you very much. I added you to my contact list. My Skype name is: therockula-s. –  Jul 28 '12 at 19:31