我正在练习在ViewController中呈现SecondViewController。
这就是我调用视图控制器的方式。
- (void)viewDidAppear:(BOOL)animated
{
NSLog(@"View Did Appear" );
SecondViewController* secViewCtrl = [ [ SecondViewController alloc ] init ];
[ self presentViewController:secViewCtrl animated:NO completion:nil ];
}
当我运行代码时。 secondViewController继续呈现。当我从ViewDidLoad调用它时,我得到了错误的访问权限。我该如何解决这个问题。?
例如:
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidAppear:(BOOL)animated
{
NSLog(@"View Did Appear" );
SecondViewController* secViewCtrl = [ [ SecondViewController alloc ] init ];
[ self presentViewController:secViewCtrl animated:NO completion:nil ];
}
SecondViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[ self.view setBackgroundColor:[ UIColor colorWithRed:64.0/255.0 green:106.0/255.0 blue:128.0/255.0 alpha:1.0 ] ];
UIButton* backBtn = [ [ UIButton alloc ] init ];
[ backBtn setFrame:CGRectMake(0, 0, 60, 44) ];
[ backBtn setImage:[ UIImage imageNamed:@"Circle.png" ] forState:UIControlStateNormal ];
[ backBtn setBackgroundColor:[ UIColor colorWithRed:0 green:0 blue:0 alpha:0.2 ] ];
[ self.view addSubview:backBtn ];
}
我可以看到SecondViewController的颜色代码。但是当我在ViewController中记录ViewDidLoad时。它不断重复。这使我不会使用任何按钮操作。
答案 0 :(得分:1)
看到发生了什么我会说SecondViewController
是ViewController
的子类。
然后当SecondViewController
出现时,它会再次调用它的超类的出现方法,这将导致这个无限循环。
如果是这种情况,只需将SecondViewController
设置为UIViewController
子类,而不是ViewController
答案 1 :(得分:0)
尝试将此代码放在方法中并从viewDidAppear调用该方法:with performSelectorOnMainThread: