Ios 在一个视图控制器中为另一个视图控制器设置变量,并切换到该视图控制器
好的,我这里有一个有趣的情况: 我有一个日历视图,该视图没有导航栏,因此我创建了另一个视图来包含日历,并在该视图中添加了导航栏 现在我有两个视图显示导航栏和日历 导航栏有一个butten,它应该显示一个“插入”控制器,但在此之前,它必须将@property从日历设置为“插入”视图控制器 总而言之: 外部视图控制器iAction->“插入”->内部日历设置属性显示“插入” 代码如下: ViewControllerCalendarContainer.hIos 在一个视图控制器中为另一个视图控制器设置变量,并切换到该视图控制器,ios,Ios,好的,我这里有一个有趣的情况: 我有一个日历视图,该视图没有导航栏,因此我创建了另一个视图来包含日历,并在该视图中添加了导航栏 现在我有两个视图显示导航栏和日历 导航栏有一个butten,它应该显示一个“插入”控制器,但在此之前,它必须将@property从日历设置为“插入”视图控制器 总而言之: 外部视图控制器iAction->“插入”->内部日历设置属性显示“插入” 代码如下: ViewControllerCalendarContainer.h #import <UIKit/UIKit
#import <UIKit/UIKit.h>
@interface ViewControllerCalendarContainer : UIViewController
- (IBAction)SeguqInsert:(id)sender;
@end
CalendarMonthViewController.h
@property (nonatomic, strong) NSDate *dateSelected; // data to send to Insert View Controller
- (void)SegueInsert; // the present "Insert View Controller Method"
CalendarMonthViewController.m
#import "CalendarMonthViewController.h"
#import "ViewControllerInsert.h"
- (void)SegueInsert {
NSDate *dateUserSelected = self.dateSelected;
ViewControllerInsert *controller = [[ViewControllerInsert alloc] initWithNibName:@"ViewControllerInsert" bundle:nil];
controller.dateSelected = dateUserSelected; // set property in Insert
[self presentViewController:controller animated:YES completion:nil]; // present
}
单击时出现运行时错误:
其视图不在窗口层次结构中
PS:我无法通过故事板进行切换,因为它使用了另一个实例,而应该设置的属性没有设置。您似乎添加了一个不需要的视图控制器。错误是因为您从未显示该视图控制器,然后尝试从中显示另一个视图控制器
将代码放入
segueinert
并移动到SeguqInsert
。然后删除CalendarMonthViewController(它可能不做任何其他事情,也没有其他代码)。Wain是对的。额外的视图控制器导致出现问题。然而,我不认为你能仅仅移动代码。您应该在导航控制器中保留一个指向日历的指针,只需在SequqInsert中设置属性即可。大概是这样的:
#import <UIKit/UIKit.h>
@interface ViewControllerCalendarContainer : UIViewController
@property (weak, nonatomic) CalendarMonthViewController *calendarViewController;
- (IBAction)SeguqInsert:(id)sender;
@end
#import "ViewControllerCalendarContainer.h"
#import "CalendarMonthViewController.h"
...
- (IBAction)SeguqInsert:(id)sender {
NSDate *dateUserSelected = self.dateSelected;
ViewControllerInsert *controller = [[ViewControllerInsert alloc] initWithNibName:@"ViewControllerInsert" bundle:nil];
controller.dateSelected = calendarViewController.dateUserSelected; // set property in Insert
[self presentViewController:controller animated:YES completion:nil]; // present
}
#导入
@界面ViewControllerCalendarContainer:UIViewController
@属性(弱,非原子)CalendarMonthViewController*calendarViewController;
-(iAction)SeguqInsert:(id)发送方;
@结束
#导入“ViewControllerCalendarContainer.h”
#导入“CalendarMonthViewController.h”
...
-(iAction)SeguqInsert:(id)发送方{
NSDate*dateUserSelected=self.dateSelected;
ViewControllerInsert*控制器=[[ViewControllerInsert alloc]initWithNibName:@“ViewControllerInsert”捆绑包:nil];
controller.dateSelected=calendarViewController.dateUserSelected;//在插入中设置属性
[self-presentViewController:控制器已设置动画:是完成:无];//存在
}
如果您担心保留日历指针,您可以随时使用协议获取信息。您的PS没有意义。没有理由不能在故事板中这样做。
#import <UIKit/UIKit.h>
@interface ViewControllerCalendarContainer : UIViewController
@property (weak, nonatomic) CalendarMonthViewController *calendarViewController;
- (IBAction)SeguqInsert:(id)sender;
@end
#import "ViewControllerCalendarContainer.h"
#import "CalendarMonthViewController.h"
...
- (IBAction)SeguqInsert:(id)sender {
NSDate *dateUserSelected = self.dateSelected;
ViewControllerInsert *controller = [[ViewControllerInsert alloc] initWithNibName:@"ViewControllerInsert" bundle:nil];
controller.dateSelected = calendarViewController.dateUserSelected; // set property in Insert
[self presentViewController:controller animated:YES completion:nil]; // present
}