所以,我对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
答案 0 :(得分:2)
有几种传递变量的方法,这是最简单的方法之一。
- (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];
}
这将创建第二个视图控制器填充值,并以两种方式之一呈现第二个视图控制器。如果第一个视图控制器嵌入在导航控制器中,那么您只需将第二个视图控制器添加到导航堆栈即可。如果第一个视图控制器不在导航控制器中,则将其显示为模态。
答案 1 :(得分:-5)
您可以使用各种方法在类之间传递变量