【问题标题】:UIActivityView in AppDelegate using protocol and delegationAppDelegate 中的 UIActivityView 使用协议和委托
【发布时间】:2014-01-09 13:50:27
【问题描述】:

我正在尝试在我的应用中实现 UIActivityView。它已经在工作了,但是我现在需要做的是在AppDelegate中创建一个方法,供其他所有类使用,所以我不需要重复代码。

在我的AppDelegate,我有以下方法:

- (void)acionarActivityIndicator {
    NSLog(@"mETODO APPDELEGATE");
    // Show the activity indicator
    UIView *view = [[UIView alloc] init];
    UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] init];

    view.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    view.frame = self.window.bounds;
    activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    CGRect frame = view.frame;
    activityIndicator.center = CGPointMake(frame.size.width/2, frame.size.height/2);
    [view addSubview:activityIndicator];
    [activityIndicator startAnimating];
    [self.window addSubview:view];
    [self.window makeKeyAndVisible];

}

该方法在@protocol.h 文件中声明。

@protocol ActivityIndicatorProtocol <NSObject>

@required

- (void)acionarActivityIndicator;

@end

现在,在 buttonClick 或其他任何地方,我需要将此视图放在活动 VC 上。为此,我设置了我的 VC 的 AppDelegatedelegate

我在viewDidLoadof VC 上有这个:

ITAppDelegate *appDelegate = [[ITAppDelegate alloc] init];
    self.delegate = appDelegate;
    NSLog(@"DELEGATE %@", self.delegate);

这是正确分配委托。委托@property在我的 VC .h 中声明如下:

@property(nonatomic, assign)id delegate;

现在,同样在viewDidLoad 中,出于测试目的,我尝试在AppDelegate 中显示UIActivityIndicatorViewby 调用方法。

[self.delegate acionarActivityIndicator];

这不起作用,我不知道为什么。我可以使用其他策略放置 activityIndi​​cators,但我真的很想学习如何正确地做到这一点。

【问题讨论】:

    标签: ios objective-c uiviewcontroller delegates


    【解决方案1】:

    尝试使用

    ITAppDelegate *appDelegate = (ITAppDelegate *)[[UIApplication sharedApplication] delegate];
    

    插入

    ITAppDelegate *appDelegate = [[ITAppDelegate alloc] init];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多