我对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
问题解决了:
在下一个按钮上我需要在移动页面之前添加变量(而不是相反 - 愚蠢的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];
答案 0 :(得分:2)
如果您需要将groupName.text
的值从Game_Info
视图控制器传递到前者以模态方式显示的Game_TDoR
视图控制器,您可以在{中声明属性{1}}保存值:
1)在Game_TDoR
@interface块中声明NSString
属性:
Game_TDoR
(请记住在实现块中合成或实现访问器方法)
2)在@property (nonatomic,copy) NSString *groupNameText;
操作中,初始化NextBTN
实例后,设置属性:
Game_TDoR
答案 1 :(得分:0)
仅包含头文件不会使变量可用。你只包括类的定义(类是用于PHP的@interface用于Obj-C)你必须实例化Game_Info并通过Game_Info实例访问变量。
答案 2 :(得分:0)
groupName是Game_Info的成员(ivar)。它的默认可见性是@protected,因此Game_Info之外的任何类都无法访问它,除非它们派生自Game_Info。要使groupName可访问,您可以创建一个公开它的属性,也可以将其设为@public。如何做到这一点记录在Xcode的大量文档中。
但是,作为对象的ivar(实例变量)的groupName只有在实际存在Game_Info的实例时才存在。我假设你的程序中有一些全局可访问的Game_Info实例,我们称之为globalGameInfo。现在,您可以使用
访问其groupNameUITextField *gName = [globalGameInfo groupName];
答案 3 :(得分:0)
在SecondViewController
中的.h文件中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方法写下这个。
-(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];