我对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];
谢谢你的回复,我只是有一点问题,除了尝试查看groupNameText一个UILabel它说'(空)'当我尝试NSLog它看到什么出来它说'格式不是字符串文字和没有格式参数' –
@ Eli.Stone你如何设置标签文本?你可以发布一些代码吗?这是我将如何记录它:'NSLog(@“groupNameText:%@”,self.groupNameText);' – albertamg
这是我在游戏信息上点击按钮所做的:Game_TDoR * screen1 = [[Game_TDoR alloc] init ]。 screen1.groupNameText = self.groupName.text; [screen1 release];其余的是:@property(nonatomic,copy)NSString * groupNameText; 但这只是返回null –