使用storyboard在模态转换时显示加载对话框

时间:2013-02-07 15:43:59

标签: iphone objective-c xcode storyboard

我第一次尝试使用故事板,我遇到了问题。

我需要在加载模态视图控制器时显示加载对话框。

我会更好地解释一下:我的导航栏上有一个按钮,当用户点击它时会出现一个模态视图控制器,但需要几秒钟,所以我需要对用户说“应用程序未被锁定” 。

更新

这是正常运行的最终代码:

-(void)showLoading {

    UIActivityIndicatorView *loading = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    loading.frame = CGRectMake(275, 0, 40, 40);
    [loading startAnimating];

    [UIView animateWithDuration:0 animations:^ {

        [self.navigationItem setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:loading]];

    } completion: ^(BOOL completed) {

        UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
        UIViewController *vc = [mainStoryboard instantiateViewControllerWithIdentifier:@"MyViewController"];
        [self presentModalViewController:vc animated:YES];

    }];

}

并在viewDidLoad

[myBarButtonItem setTarget:self];
[myBarButtonItem setAction:@selector(visualizzaCaricamento)];

1 个答案:

答案 0 :(得分:1)

一种简单的方法是使用内置的UIActivityIndicator。它有大大小小的品种。您可以将其放在所有内容的视图中,也可以将其添加到UINavigationBar

这里有一些代码可以用加载微调器替换按钮。

UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[indicator startAnimating];

[self.navigationBar setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:indicator]];

<强>更新

// In your viewDidLoad or somewhere like that.
[self.navigationBar setRightBarButtonItem:[[UIBarButtonItem alloc] initWithTitle:@"Do" style:UIBarButtonItemStyleBordered target:self action:@selector(doSomething:)]];

- (void)doSomething:(id)sender {
    // Show spinner
    UIActivityIndicatorView *indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
    [self.navigationBar setRightBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:indicator]];
    [indicator startAnimating];

    // ... Load View
}