我在导航控制器中有两个视图控制器。 在第一个视图控制器中,我有两个按钮。它们都使用Push segue调用第二个视图控制器,但是:
编辑:
我不知道我的问题是否清楚:现在我知道如何使用prepareForSegue将数据从第一个视图控制器传递到第二个视图控制器,但我真正需要的是从数据传递数据(选择的日期)第二个视图控制器到第一个,如何在没有prepareForSegue的情况下执行它(按下确定时)?
EDIT2:
我做到了。这太简单了,伙计...... 我决定使用modal segue:
Firstviewcontroller.h:
+(FirstViewController *)getInstance;
Firstviewcontroller.m:
static FirstViewController *instance =nil;
+(FirstViewController *)getInstance
{
return instance;
}
并在其ViewDidLoad中:
instance = self;
Secondviewcontroller.m,在OkButton IBAction中:
SecondViewController *secondViewController = [SecondViewController getInstance];
//...
//modify what I need to modify in secondviewcontroller
//...
[self dismissModalViewControllerAnimated:YES];
就是这样。 无论如何,谢谢大家。
答案 0 :(得分:3)
为故事板中的每个segue分配标识符并实施
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Make sure your segue name in storyboard is the same as this line
if ([[segue identifier] isEqualToString:@"YOUR_SEGUE_NAME_HERE"])
{
// Get reference to the destination view controller
YourViewController *vc = [segue destinationViewController];
[vc setDelegate:self];
// Pass any objects to the view controller here, like...
[vc setMyObjectHere:object];
}
}
有关How to use storyboard and pass value check this article或this discussion on stackoverflow
的详情对于第二个问题,您可以使用delegate pattern
IN SecondViewController.h
@protocol SomethingDelegate <NSObject>
- (void)dateChanged:(NSString *)dateStr; //you can use NSDate as well
@end
@interface ViewController2 : UIViewController
@property(weak) id<SomethingDelegate> delegate;
@end
<。>文件中的
-(void) OkClicked{
[_delegate dateChanged:@"YOUR_DATE_VALUE"];
}
在FirstViewController.h
#import "SecondViewController.h"
@interface FirstViewController : UIViewController<SomethingDelegate>
in .m
-(void)dateChanged:(NSString *)dateStr{
// do whatever you need with dateStr
//also i made some change in prepareForSegue method
}
注意: - 注意VC的命名召集
答案 1 :(得分:0)
问候
约翰