【问题标题】:Best way to pass variables between views在视图之间传递变量的最佳方式
【发布时间】: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


    【解决方案1】:

    如果您需要将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;
    

    【讨论】:

    • 感谢您的回复,我只是有一个小问题,除了尝试在 UILabel 中查看 groupNameText 时显示“(Null)”以及当我尝试 NSLog 它时,一切似乎都工作正常看看结果是什么“格式化不是字符串文字,也没有格式参数”
    • @Eli.Stone 你如何设置标签文本?你能发布一些代码吗?这就是我记录它的方式:NSLog(@"groupNameText: %@", self.groupNameText);
    • 这是我在点击按钮的游戏信息中所做的:Game_TDoR *screen1 = [[Game_TDoR alloc] init]; screen1.groupNameText = self.groupName.text;[screen1 release];然后剩下的是:@property (nonatomic,copy) NSString *groupNameText;但这只是返回 null
    • 我假设您在发布 screen1 之前以模态方式呈现它 ([self presentModalViewController:screen1 animated:YES];),对吧?另外,如果你在Game_TDoRviewDidLoad中登录groupNameTextNSLog(@"groupNameText: %@", self.groupNameText);),输出是什么?
    • 好吧,我现在有了 :) 所以这是我必须发送到下一个视图 Game_TDoR *screen1 = [[Game_TDoR alloc] initWithNibName:nil bundle:nil]; screen1.groupNameText = self.groupName.text;[self presentModalViewController:screen1 animated:YES];[screen1 release]; 并从另一个视图获取的整个代码:NSLog(@"Logged: %@",self.groupNameText);NSString *msg = [[NSString alloc] initWithFormat:@"Hello, %@",[self.groupNameText capitalizedString]];[Testlbl setText:msg];[msg release];) 但是我创建的标签和 NSLog 返回'(空)'
    【解决方案2】:

    仅仅包含头文件不会使变量可用。您只包括类的定义(类用于 PHP,@interface 用于 Obj-C)您必须实例化 Game_Info 并通过 Game_Info 的实例访问变量。

    【讨论】:

    • @Eli 嗯,你应该复习一些教程。实例化类的一种方法是Game_Info *myGameInfo = [[Game_Info alloc] init];
    【解决方案3】:

    groupName 是 Game_Info 的成员 (ivar)。它的默认可见性是@protected,因此Game_Info 之外的任何类都无法访问它,除非它们派生自Game_Info。要使 groupName 可访问,您可以创建一个公开它的属性,也可以将其设为 @public。 Xcode 的大量文档中记录了这是如何完成的。

    但是 groupName 作为对象的 ivar(实例变量),只有在实际上存在 Game_Info 的实例时才存在。我假设你的程序中有一些全局可访问的 Game_Info 实例,我们称之为 globalGameInfo。现在您可以使用

    访问其 groupName
    UITextField *gName = [globalGameInfo groupName];
    

    【讨论】:

      【解决方案4】:

      把这个放在 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];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-09-10
        • 1970-01-01
        相关资源
        最近更新 更多