iphone – 在视图之间传递变量的最佳方法

iphone – 在视图之间传递变量的最佳方法,第1张

概述我是 xcode&的新手. Objective-C(来自 PHP)我已经开始玩了,我发现很难在视图之间传递变量这是我到目前为止所拥有的: Game_info.h #import <UIKit/UIKit.h>@interface Game_Info : UIViewController { IBOutlet UITextField *groupName; IBOutlet UI 我是 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];
解决方法 如果您需要将groupname.text的值从Game_Info视图控制器传递到Game_TDoR视图控制器(由前者以模态方式呈现),您可以在Game_TDoR中声明一个属性来保存值:

1)在Game_TDoR @interface块中声明Nsstring属性:

@property (nonatomic,copy) Nsstring *groupnameText;

(记得在实现块中合成或实现访问器方法)

2)在NextBTN *** 作中,初始化Game_TDoR实例后,设置属性:

Game_TDoR *screen = [[Game_TDoR alloc] init...];screen.groupnameText = self.groupname.text;
总结

以上是内存溢出为你收集整理的iphone – 在视图之间传递变量的最佳方法全部内容,希望文章能够帮你解决iphone – 在视图之间传递变量的最佳方法所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1061755.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存