我正在使用iOS Recipes PragProg书中的食谱,这是我正在使用的原始代码部分:

- (void)addActivityIndicatorView {

    activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activityIndicatorView.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin;
    CGRect aiFrame = activityIndicatorView.frame;
    CGFloat originX = (self.view.bounds.size.width - aiFrame.size.width) / 2;
    CGFloat originY = (self.view.bounds.size.height - aiFrame.size.height) / 2;
    aiFrame.origin.x = floorl(originX);
    aiFrame.origin.y = floorl(originY);
    activityIndicatorView.frame = aiFrame;
    [self.view addSubview:activityIndicatorView];
}


我意识到我想要在其他通过Web加载数据的控件上的多个activityIndi​​catorViews的activityindicator代码,所以这是我的实现以使代码可重用,并且没有像上面的代码那样为一个viewcontroller显示控件上的activityIndi​​cator视图。是的,我确实对使用它的每个控件都介入了此代码,然后调用[activityIndi​​cator startAnimating];我传递给此函数的activityIndi​​cator。猜猜我要问的是我应该在代码的其他地方看什么?

- (void)addActivityIndicatorView:(UIActivityIndicatorView *)acitivityIndicator toView:(UIView *)view {

    acitivityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    acitivityIndicator.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin;
    CGRect aiFrame = acitivityIndicator.frame;
    CGFloat originX = (view.bounds.size.width - aiFrame.size.width) / 2;
    CGFloat originY = (view.bounds.size.height - aiFrame.size.height) / 2;
    aiFrame.origin.x = floorl(originX);
    aiFrame.origin.y = floorl(originY);
    acitivityIndicator.frame = aiFrame;
    [view addSubview:acitivityIndicator];
}

最佳答案

这样写你的方法:

- (void)addActivityIndicatorView:(UIActivityIndicatorView **)activityIndicator toView:(UIView *)view {

    UIActivityIndicatorView * activeInd = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
    activeInd.autoresizingMask = UIViewAutoresizingFlexibleTopMargin |
    UIViewAutoresizingFlexibleRightMargin |
    UIViewAutoresizingFlexibleBottomMargin |
    UIViewAutoresizingFlexibleLeftMargin;
    CGRect aiFrame = activeInd.frame;
    CGFloat originX = (view.bounds.size.width - aiFrame.size.width) / 2;
    CGFloat originY = (view.bounds.size.height - aiFrame.size.height) / 2;
    aiFrame.origin.x = floorl(originX);
    aiFrame.origin.y = floorl(originY);
    activeInd.frame = aiFrame;

    *activityIndicator = activeInd;
    [view addSubview:*activityIndicator];
}


如果要使用上述方法,请将对UIActivityIndicatorView *的引用传递给该方法,例如:

-(void) viewDidLoad
{
  [super viewDidLoad];

  UIActivityIndicatorView * ai;
  [self addActivityIndicatorView: &ai toView: self.view];

  activityIndicator = ai;
}


现在,每当您希望它开始动画时:

[activityIndicator startAnimating];

关于objective-c - 试图将UIActivityIndi​​catorView添加到 View 中,不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11068586/

10-09 09:14