【问题标题】:Passing variables and switching views iOS/Xcode传递变量和切换视图 iOS/Xcode
【发布时间】:2014-07-14 00:09:00
【问题描述】:

所以,我对 iOS/Obj-C 和 Xcode 非常陌生,我正在尝试通过构建一个简单的选项卡式视图应用程序来学习,该应用程序采用一些用户变量,将用户移动到下一个视图并显示变量。

目前,我有第一个视图 - 用户选择两个日期。我已成功将这两个日期记录到控制台。我还不太明白在视图之间移动的概念。

我需要帮助的是 - 在 - (IBAction)submitDates 函数中,将用户移动到下一个视图并将变量也传递到该视图 - 并将变量记录到控制台。

注意:第二个视图文件(JPSecondViewController.m 和 JPSecondViewController.h 尚未触及)。

提前非常感谢任何帮助/指导!

我的 JPFirstViewController.m 文件

//  JPFirstViewController.m
//  Vacay
//

#import "JPFirstViewController.h"
#import "JPSecondViewController.h"

@interface JPFirstViewController ()

@end

@implementation JPFirstViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)submitDates {


    //Save the selected date variables
    NSDate *dateFromPicker = [_fromDate date];
    NSDate *endDateFromPicker = [_endDate date];
    NSLog(@"From date: %@ and end date: %@", dateFromPicker, endDateFromPicker);

    //Move user to second view controller

}

@end

我的 JPFirstViewController.m 文件

//  JPFirstViewController.h
//  Vacay
/

#import <UIKit/UIKit.h>
#import "JPSecondViewController.h"

@interface JPFirstViewController : UIViewController
@property (strong, nonatomic) IBOutlet UIDatePicker *endDate;
@property (strong, nonatomic) IBOutlet UIDatePicker *fromDate;
- (IBAction)submitDates;

@end

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    有几种传递变量的方法,这里是最简单的一种。

    - (IBAction)submitDates {
        //Save the selected date variables
        NSDate *dateFromPicker = [_fromDate date];
        NSDate *endDateFromPicker = [_endDate date];
        NSLog(@"From date: %@ and end date: %@", dateFromPicker, endDateFromPicker);
    
        //Create an instance of the second view controller
        // If you are using NIBs
        JPSecondViewController *secondViewController = [[JPSecondViewController alloc] init];
    
        //// If you are using storyboards, then you will need to know what the storyboard identifier is for JPSecondViewController. 
        //NSString *identifier = @"<Second Storyboard Identifier>";
        //JPSecondViewController *secondViewController = [self.storyboard instantiateViewControllerWithIdentifier:identifier];
    
        //Fill in all the data need for the second view controller
        secondViewController.fromDate = dateFromPicker;
        secondViewController.endDate = endDateFromPicker;
    
        //Show the second view controller
        // Option 1: Add the second view controller to a navigation controller
        [self.navigationController pushViewController:secondViewController animated:YES];
    
        //// Option 2: Show the second view controller as a modal view controller
        //[self presentViewController:secondViewController animated:YES completion:nil];
    }
    

    这将创建第二个视图控制器填充值并以两种方式之一呈现第二个视图控制器。如果第一个视图控制器嵌入在导航控制器中,那么您只需将第二个视图控制器添加到导航堆栈。如果第一个视图控制器不在导航控制器中,则将其呈现为模式。

    【讨论】:

    • 谢谢,变量有一些错误('JPSecondViewController *' 类型的对象上找不到属性'fromDate'),但我应该能够破解它!
    • 我的第二个视图也是黑色的。我已经向 viewDidLoad 添加了一个日志,它确认它加载了第二个视图。有什么建议吗?谢谢杰弗里!
    • @user2656127 您需要向JPSecondViewController 添加属性来保存开始和结束日期。
    • @user2656127 我更新了我对故事板的回答,以防您使用故事板。
    【解决方案2】:

    您可以使用多种方法在类之间传递变量

    1. 单身人士(我更喜欢)
    2. NSUserDefaults

    【讨论】:

    • 太棒了!如果这不是太麻烦 - 你介意帮助我编写代码以使我在视图切换方面朝着正确的方向前进吗?然后我将这两个变量实现为全局变量。
    • 确定很高兴能提供帮助,这是您正在寻找的灯,我已经在我的应用程序中使用了它:stackoverflow.com/a/19604873/3551355
    • 你忘记了核心数据。 :) 说真的,看看 Jeffery Thomas 的回答。
    • @AhmetCevdet,您还应该阅读 trojanfoe 在他的回答中写的内容。
    • @vikingosegundo okk 谁是特洛伊木马?,所以我可以阅读他在回答中写的内容
    猜你喜欢
    • 1970-01-01
    • 2011-07-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 2011-09-05
    • 2012-04-25
    • 1970-01-01
    相关资源
    最近更新 更多