按钮单击时的modalPresentationStyle

时间:2014-05-12 09:53:01

标签: ios objective-c

我有一个_exampleButton:

_exampleButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_exampleButton setBackgroundColor:[UIColor redColor]];
    [_exampleButton addTarget:self action:@selector(certificatesButtonTouched) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_exampleButton];

和行动:

    -(void)certificatesButtonTouched
{
    if(!_certificatesWindow)
    {
        _certificatesWindow = [[AWCertificatesViewController alloc] init];


        UINavigationController *navController = [[UINavigationController alloc]
                                                 initWithRootViewController:_certificatesWindow];
        navController.modalPresentationStyle = UIModalPresentationFormSheet;
        [self presentViewController:navController animated:YES completion:nil];
        [_certificatesWindow release];

    }
    else {

        [_certificatesWindow.view removeFromSuperview];
        [_certificatesWindow release];
        _certificatesWindow = nil;

        }
    }

这提供了来自其他类的模态视图控制器中的窗口:

    - (void)viewDidLoad
{
    [super viewDidLoad];

    UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]
                                   initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
                                   target:self
                                   action:@selector(cancel:)];
    self.navigationItem.leftBarButtonItem = cancelItem;


    UITableView *table = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame] style:UITableViewStylePlain];
    [self.view addSubview:table];
    _tableFromButton = table;
}

- (void)cancel:(id)sender
{

    [self.presentingViewController dismissViewControllerAnimated:YES
                                                      completion:nil];
}

但单击取消按钮后,模态视图控制器视图消失,但如果我再次单击_exampleButton - 它将不会出现,所以我必须再次单击它以再次显示模态视图控制器。问题是什么?

4 个答案:

答案 0 :(得分:0)

问题是在您点按“取消”后,当您点按_certificatesWindow时,certificatesButtonTouched仍然存在。它将转到首次点按上的 Else 语句,以生成_certificatesWindow = nil

点击取消时如果不需要_certificatesWindow,您可能需要执行以下操作: -

- (void)cancel:(id)sender
{

[self.presentingViewController dismissViewControllerAnimated:YES
                                                  completion:nil];
  [_certificatesWindow.view removeFromSuperview];
    [_certificatesWindow release];
    _certificatesWindow = nil;
}

答案 1 :(得分:0)

在view do load方法中分配这样的视图

 if(!_certificatesWindow)
    {
        _certificatesWindow = [[ViewController1 alloc] init];
    }

     -(void)certificatesButtonTouched
{
    UINavigationController *navController = [[UINavigationController alloc]
                                             initWithRootViewController:_certificatesWindow];
    navController.modalPresentationStyle = UIModalPresentationFormSheet;
    [self presentViewController:navController animated:YES completion:nil];
}

答案 2 :(得分:0)

这样做是因为这是你的代码告诉它做的......

您在-(void)cancel:(id)sender中关闭了视图控制器,但这不会导致_certificatesWindow iVar在您的第一个视图控制器中变为零。因此,当您再次触摸exampleButton时,它将执行else子句并清理_certificatesWindow视图控制器。

您应该使用委托或代码块让第一个视图控制器关闭第二个视图,或者从certificatesButtonTouched中删除if / else测试。另一种方法是修改此方法,以便_certificatesWindow不为零时重复使用 -

   -(void)certificatesButtonTouched
    {
      if(!_certificatesWindow)
      {
         _certificatesWindow = [[AWCertificatesViewController alloc] init];
      }

         UINavigationController *navController = [[UINavigationController alloc]
                                                 initWithRootViewController:_certificatesWindow];
         navController.modalPresentationStyle = UIModalPresentationFormSheet;
         [self presentViewController:navController animated:YES completion:nil];

    }

但根据第二个视图控制器显示的内容,这可能是也可能不合适。

我还建议你看看如果可能的话转换到ARC

答案 3 :(得分:0)

用这个

替换你的代码
    -(void)certificatesButtonTouched
{
    if(!_certificatesWindow)
    {
        _certificatesWindow = [[AWCertificatesViewController alloc] init];
    }

        UINavigationController *navController = [[UINavigationController alloc]
                                                   initWithRootViewController:_certificatesWindow];
        navController.modalPresentationStyle = UIModalPresentationFormSheet;
        [self presentViewController:navController animated:YES completion:nil];
        [_certificatesWindow release];

    }