在视图之间传递变量的最佳方法 [英] Best way to pass variables between views
问题描述
我对xcode& Objective-C(来自PHP)我已经开始玩了,我发现很难在视图之间传递变量这是我到目前为止:
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];
}
}
然后在另一个视图/ .h中/ .m文件我试图进入'groupName'属性。
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.
}
所以我想做的是在第一个视图页面(Game_info)有一个输入文本框,我试图将其传递给另一个视图页面上的标签(Game_TDoR)
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)
这是NSLog中出现的内容(注意第二个)页面(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
问题解决了:
在下一个按钮上我需要在移动页面之前添加变量(不是相反的方式) - 愚蠢的noobish做的事情......)
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];
推荐答案
如果你需要传递<$ c的值$ c> groupName.text 从 Game_Info
视图控制器到 Game_TDoR
视图控制器,由前者以模态方式呈现,您可以在 Game_TDoR
中声明一个属性来保存该值:
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)在 Game_TDoR
@interface块中声明 NSString
属性:
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)在 NextBTN
操作中,初始化 Game_TDoR
实例后,设置属性:
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;
这篇关于在视图之间传递变量的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!