0

我第一次尝试使用情节提要,但遇到了问题。

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

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

更新

这是正常工作的最终代码:

-(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)];
4

1 回答 1

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
}
于 2013-02-07T15:59:13.810 回答