【问题标题】:Show loading dialog while modal transition using storyboard使用情节提要在模态转换时显示加载对话框
【发布时间】:2013-02-07 15:43:58
【问题描述】:

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

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

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

更新

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

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

【问题讨论】:

    标签: iphone objective-c xcode storyboard


    【解决方案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
    }
    

    【讨论】:

    • 我把它放在 viewWillDisappear 方法中,有没有更好的方法来实现呢?感谢您的回答!
    • 要么在那里,要么你可以把它放在实际的按钮点击方法中。
    • 我正在尝试这个 [myBarButtonItem setAction:@selector(showLoading)];但不起作用
    • 我会用更多代码更新我的答案。这不是应该的。
    • 我已经完成了你写的。现在的问题是如何加载视图,因为我正在使用情节提要;现在我正在尝试类似 [self performSegueWithIdentifier: @"nextView" sender: self];我在正确的道路上?
    猜你喜欢
    • 2012-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多