我有一个UInavigationController
堆栈。在RootViewController
我有按钮。当我单击按钮时,将推送一个新的viewcontroller,并显示UIDatePicker
。问题是当导航到视图控制器时,日期选择器显示1秒然后突然消失,整个屏幕变黑。这是代码
//rootviewcontroller
- (IBAction)dateAction:(id)sender
{
self.another = [[AnotherViewController alloc]initWithButtonWithTag:self.dateButton.tag andDelegate:self];
[self.navigationController pushViewController:self.another animated:YES];
self.another.navigationItem.title = @"DateName";
}
//second viewcontroller
- (void)viewDidload
{
self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
[self.pickerView addTarget:self action:@selector(datePickerDidChangeDate:) forControlEvents:UIControlEventValueChanged];
self.pickerView.datePickerMode = UIDatePickerModeDate;
self.pickerView.hidden = NO;
self.pickerView.date = [NSDate date];
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateStyle = NSDateFormatterLongStyle;
[self.view addSubview:self.pickerView];
}
帮助将不胜感激
答案 0 :(得分:1)
试试这段代码......
第一个视图控制器
- (void)viewDidLoad
{
[super viewDidLoad];
NSArray *temp=[NSArray arrayWithObjects:@"button1" ,@"button2" ,@"button3",nil];
for (int i=0; i<=2; i++)
{
_likeBtn=[UIButton buttonWithType:UIButtonTypeCustom];
[_likeBtn setFrame:CGRectMake(20+(i*80), 220, 102, 20 )];
[_likeBtn setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
[_likeBtn setTitle:[temp objectAtIndex:i] forState:UIControlStateNormal];
[_likeBtn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[_likeBtn setTag:i];
[self.view addSubview:_likeBtn];
}
// Do any additional setup after loading the view from its nib.
}
- (IBAction)action:(id)sender
{
UIButton *button=(UIButton *)sender;
NSLog(@"%d",button.tag);
secViewController *obj = [[secViewController alloc]init];
obj.tagValue=button.tag;
[self.navigationController pushViewController:obj animated:YES];
obj.navigationItem.title = @"DateName";
}
第二个视图.h文件
@property(nonatomic,assign) NSInteger *tagValue;
.m文件
- (void)viewDidLoad
{
[super viewDidLoad];
NSInteger *tag=self.tagValue;
if (tag==0)
{
//tableView
NSLog(@"tableview");
}
else
{
//datepicker
NSLog(@"picker");
self.pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 250, 325, 300)];
[self.pickerView addTarget:self action:@selector(datePickerDidChangeDate:) forControlEvents:UIControlEventValueChanged];
self.pickerView.datePickerMode = UIDatePickerModeDate;
self.pickerView.hidden = NO;
self.pickerView.date = [NSDate date];
self.formatter = [[NSDateFormatter alloc] init];
self.formatter.dateStyle = NSDateFormatterLongStyle;
[self.view addSubview:self.pickerView];
}
// Do any additional setup after loading the view from its nib.
}
-(void)datePickerDidChangeDate:(id)sender
{
NSLog(@"%@",self.pickerView.date);
}
让我知道是否满意......