2011-07-16 43 views
2

我对xcode非常陌生& Objective-C(来自PHP)我已经开始玩了,并且发现很难在视图之间传递变量,这是我迄今为止所拥有的:在视图之间传递变量的最佳方式

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]; 

    } 
} 

然后在另一个视图/ .h/.m文件中,我正在尝试访问'groupName'属性。

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. 
} 

那么什么,我试图做的是第一个视图页面(Game_info)有一个文本输入框和IM试图传递的一个标签上的另一个观点上页面(Game_TDoR)

这是在的NSLog出来(注意,第二页(Game_TDoR)出来首先在日志中。

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 

问题解决了:

下一步按钮我需要添加变量之前我搬到网页(而不是其他方式 - 傻noobish的事情...)

 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]; 

回答

2

如果您需要从Game_Info视图控制器到Game_TDoR视图控制器,它是由前模态呈现合格groupName.text的价值,你可以在Game_TDoR声明一个属性来保存的值:

1)声明NSString财产Game_TDoR @interface块:

@property (nonatomic,copy) NSString *groupNameText; 

(请记住,合成或实现在执行块存取方法)

2)在NextBTN动作,之后您初始化Game_TDoR例如,设置属性:Game_Info的

Game_TDoR *screen = [[Game_TDoR alloc] init...]; 
screen.groupNameText = self.groupName.text; 
+0

谢谢你的回复,我只是有一点问题,除了尝试查看groupNameText一个UILabel它说'(空)'当我尝试NSLog它看到什么出来它说'格式不是字符串文字和没有格式参数' –

+0

@ Eli.Stone你如何设置标签文本?你可以发布一些代码吗?这是我将如何记录它:'NSLog(@“groupNameText:%@”,self.groupNameText);' – albertamg

+0

这是我在游戏信息上点击按钮所做的:Game_TDoR * screen1 = [[Game_TDoR alloc] init ]。 screen1.groupNameText = self.groupName.text; [screen1 release];其余的是:@property(nonatomic,copy)NSString * groupNameText; 但这只是返回null –

0

仅仅包括头文件不会使变量可用。你只包括类的定义(类是PHP的什么@interface是Obj-C)你必须实例化Game_Info并通过Game_Info的实例访问变量。

+0

我该怎么做? –

+0

@Eli嗯,你应该刷一些教程。一种实例化类的方法,你做'Game_Info * myGameInfo = [[Game_Info alloc] init];' – pixelfreak

0

组名是成员(伊娃)。它的默认可见性是@protected,所以Game_Info之外的任何类都无法访问它,除非它们来自Game_Info。要使groupName可访问,您可以创建一个公开它的属性,也可以将其设置为@public。这是如何完成的,在Xcode的大量文档中有记录。

但是,作为对象的ivar(实例变量)的groupName只有在实际上存在Game_Info的实例时才存在。我假设你的程序中有一些全局可访问的Game_Info实例,我们称它为globalGameInfo。现在,您可以访问使用

UITextField *gName = [globalGameInfo groupName]; 
0

借此它的组名中的.h文件中SecondViewController

NSString *strABC; 

进行以下功能SecondViewController.m

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

现在在第一视图控制器不喜欢这个:

SecondViewController.m *objSecond = [[SecondViewController.m] initwithNibName:@"secondView.xib" bundle:nil]; 

[objSecond setString:@"Comment Controller"]; 
[self.navigationController pushViewController:objSecond animated:YES]; 
[objSecond release]; 

现在,在secondViewController viewWillAppear Method中写下这个。

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

请检查拼写错误,因为我手写这个。希望这个帮助。

如果你不使用navigationContoller,那么你可以做这样的事情。

SecondViewControler *objSecond = [[SecondViewController] initwithNibName:@"secondview.xib" bundle:nil]; 
[objSecond setUserInput:txtUserInput.text]; 
[objSecond viewWillAppear:YES]; 
[self.view addSubview:objSecond]; 
[objSecond release]; 
相关问题