Best way to pass variables between views

前端 未结 4 1392
走了就别回头了
走了就别回头了 2020-12-21 12:56

I am very new to xcode & Objective-C (having come from PHP) i have started to play around and am finding it very hard to pass variables between views this is what i have

相关标签:
4条回答
  • 2020-12-21 13:27

    Merely including the header file will not make the variable available. You are only including the definition of the class (class is for PHP what @interface is for Obj-C) You have to instantiate Game_Info and access the variable via the instance of Game_Info.

    0 讨论(0)
  • 2020-12-21 13:31

    Take this in .h file in SecondViewController

    NSString *strABC;
    

    Make below function in SecondViewController.m

    -(void)setString:(NSString *)strEntered{
        strABC=strEntered;
    }
    

    Now In First view controller do like this:

    SecondViewController.m *objSecond = [[SecondViewController.m] initwithNibName:@"secondView.xib" bundle:nil];
    
    [objSecond setString:@"Comment Controller"];
    [self.navigationController pushViewController:objSecond animated:YES];
    [objSecond release];
    

    Now, In secondViewController viewWillAppear Method write this.

    -(void)viewWillAppear:(BOOL)animated{
          lblUserInput.text = strABC;
    }
    

    Please check spelling mistakes as I hand written this. Hope this help.

    If you are not using navigationContoller then you can do something like this.

    SecondViewControler *objSecond = [[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil];
    [objSecond setUserInput:txtUserInput.text];
    [objSecond viewWillAppear:YES];
    [self.view addSubview:objSecond];
    [objSecond release];
    
    0 讨论(0)
  • 2020-12-21 13:50

    If you need to pass the value of groupName.text from the Game_Info view controller to the Game_TDoR view controller, which is presented modally by the former, you can declare a property in Game_TDoR to hold the value:

    1) Declare a NSString property in Game_TDoR @interface block:

    @property (nonatomic,copy) NSString *groupNameText;
    

    (Remember to synthesize or implement the accessor methods in the implementation block)

    2) In NextBTN action, after you initialize your Game_TDoR instance, set the property:

    Game_TDoR *screen = [[Game_TDoR alloc] init...];
    screen.groupNameText = self.groupName.text;
    
    0 讨论(0)
  • 2020-12-21 13:51

    groupName is a member (ivar) of Game_Info. Its default visibility is @protected, so any classes outside Game_Info can't access it, unless they derive from Game_Info. To make groupName accessible, you can either create a property that exposes it, or you can make it @public. How this is done is documented in the vast documentation for Xcode.

    But groupName, being an ivar (instance variable) of an object, only exists if there is in fact an instance of the Game_Info. I assume you have some globally accessible instance of Game_Info in your program, let's call it globalGameInfo. Now you can access its groupName using

    UITextField *gName = [globalGameInfo groupName];
    
    0 讨论(0)
提交回复
热议问题