Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

iphone - Best way to pass variables between views

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 so far:

Game_info.h

#import <UIKit/UIKit.h>

@interface Game_Info : UIViewController {
    IBOutlet UITextField *groupName;
    IBOutlet UISegmentedControl *gameType;

}

@property (nonatomic,retain) IBOutlet UITextField *groupName;


- (IBAction) GameTypePicker;
- (IBAction) KeyboardHide;
- (IBAction) BackBTN;
- (IBAction) NextBTN;

@end

Game_Info.m

#import "I_Dare_YouViewController.h"
#import "Game_Info.h"
#import "Game_TDoR.h"


    @implementation Game_Info
    @synthesize groupName;


// Next Button
-(IBAction) NextBTN{
    if ([groupName.text length] == 0) {
        // Alert
        UIAlertView *alert = [[UIAlertView alloc] 
                              initWithTitle:@"Group Name" 
                              message:@"Please enter a group name" 
                              delegate:self 
                              cancelButtonTitle:@"OK" 
                              otherButtonTitles:nil
                              ];
        [alert show];
        [alert release];
    }else if([groupName.text length] < 3){
        // Alert
        UIAlertView *alert = [[UIAlertView alloc] 
                              initWithTitle:@"Group Name" 
                              message:@"Please enter a name longer than 3 characters" 
                              delegate:self 
                              cancelButtonTitle:@"OK" 
                              otherButtonTitles:nil
                              ];
        [alert show];
        [alert release];
    }else{
        Game_TDoR *screen = [[Game_TDoR alloc] initWithNibName:nil bundle:nil];
        screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentModalViewController:screen animated:YES];
        [screen release];

        Game_TDoR *screen1 = [[Game_TDoR alloc] initWithNibName:nil bundle:nil];
        NSLog(@"Game_Info: %@",self.groupName.text);
        screen1.groupNameText = self.groupName.text; 
        [self presentModalViewController:screen1 animated:YES];
        [screen1 release];

    }
}

Then in another view / .h / .m file i am trying to get to the 'groupName' property.

Game_TDoR.m

#import "I_Dare_YouViewController.h"
#import "Game_Info.h"
#import "Game_TDoR.h"
@implementation Game_TDoR
@synthesize groupNameText,Testlbl;


- (void)viewDidLoad
{
    NSLog(@"Game_TDoR: %@",self.groupNameText);
    NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@",[self.groupNameText capitalizedString]];
    [Testlbl setText:msg];
    [msg release];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

So what i am trying to do is on the first view page (Game_info) there is an input text box and im trying to pass that to a label on another view page (Game_TDoR)

This is what comes out in the NSLog (note that the second page (Game_TDoR) comes out first in the log.

2011-07-17 00:25:34.765 I Dare You[3941:207] Game_TDoR: (null)
2011-07-17 00:25:34.774 I Dare You[3941:207] Game_Info: Name

Problem solved:

On the next button i needed to add the variable before i moved page (not the other way around - silly noobish thing to do...)

        Game_TDoR *screen1 = [[Game_TDoR alloc] initWithNibName:nil bundle:nil];
        NSLog(@"Game_Info: %@",self.groupName.text);
        screen1.groupNameText = self.groupName.text; 
        [self presentModalViewController:screen1 animated:YES];
        [screen1 release];

        Game_TDoR *screen = [[Game_TDoR alloc] initWithNibName:nil bundle:nil];
        screen.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        [self presentModalViewController:screen animated:YES];
        [screen release];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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;

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...