【发布时间】:2011-07-17 07:29:54
【问题描述】:
我是 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) 上有一个输入文本框,我试图将它传递给另一个视图页面 (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
问题解决了:
在下一个按钮上,我需要在移动页面之前添加变量(而不是相反 - 愚蠢的事情要做......)
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];
【问题讨论】:
标签: iphone objective-c xcode