通过UIAlertViewController呈现View控制器

时间:2016-07-05 10:32:06

标签: ios objective-c uialertcontroller

我有一个View控制器1,我点击一个Button就会显示UIAlertController。然后我在UIAlertController上展示一个View Controller 2。当我解除所呈现的视图控制器2时,UIAlertController的位置似乎像图像中的那个一样被改变。如何解决?

- (void)viewDidLoad
 {
        [super viewDidLoad];
         UIButton *button1 = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
        [button1 setCenter:self.view.center];
        [button1 addTarget:self action:@selector(showAlert) forControlEvents:UIControlEventTouchUpInside];
        [button1 setTitle:@"Present Alert" forState:UIControlStateNormal];
       [button1 setBackgroundColor:[UIColor redColor]];
      [self.view addSubview:button1];  
    }


-(void)showAlert
    {
      UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"Alert 1" message:@"1" preferredStyle:UIAlertControllerStyleAlert];
      [alert1 addAction:[UIAlertAction actionWithTitle:@"Ok" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
         }] ];
      [self presentViewController:alert1 animated:YES completion:nil];

       ViewController2 *vc2 = [[ViewController2 alloc]init];
       [alert1 presentViewController:vc2 animated:YES completion:nil];

在ViewController2内部,

 -(void)viewDidLoad
        {
           [super viewDidLoad];
           self.view.backgroundColor = [UIColor yellowColor];

          UIButton *dismissVC = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 200, 100)];
           [dismissVC setCenter:self.view.center];
           [dismissVC setTitle:@"Dismiss" forState:UIControlStateNormal];
           [dismissVC addTarget:self action:@selector(dismiss) forControlEvents:UIControlEventTouchUpInside];
           [self.view addSubview:dismissVC];
         }

-(void)dismiss
    {
      [self dismissViewControllerAnimated:YES completion:nil];
   }

Alert controller Image

0 个答案:

没有答案
相关问题