iphone – 在视图之间传递变量的最佳方法
发布时间:2020-12-14 20:04:19  所属栏目:百科  来源:网络整理 
            导读:我是 xcode的新手. Objective-C(来自 PHP)我已经开始玩了,我发现很难在视图之间传递变量这是我到目前为止所拥有的: Game_info.h #import UIKit/UIKit.h@interface Game_Info : UIViewController { IBOutlet UITextField *groupName; IBOutlet UISegmentedCo
                
                
                
            | 
                         
 我是 
 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; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!  | 
                  
