2013-12-17 18 views
-1

我有一個類SpinnerController具有對活動的指標如何在多個視圖中使用一個活動指示器?

-(void)StartSpinner 
{ 

    [spinner startAnimating]; 

    spinner.hidden=YES; 

     spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(141.0, 190.0, 

     80.0, 80.0)]; 

     [self.view addSubview:spinner]; 
    NSLog(@"Spinner running"); 
    } 

    -(void)StopSpinner 
    { 

    [spinner stopAnimating]; 

    spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(141.0, 190.0, 80.0, 

    80.0)]; 

    [self.view addSubview:spinner]; 

    } 

方法我有其他8個viewControllers,我想使用上述功能使用活動的指標。

我該怎麼做?

+0

將此函數添加到其他所有視圖c ontroller ..是不是很簡單? – AsifHabib

+0

我想從一個類到另一個類使用這個函數。 – user3109827

回答

1

您可以創建一個活動指示器的共享實例。它將幫助您創建單個實例並管理整個代碼。你可以參考MBProgressHUD的代碼。

1

有兩件事會幫助您更好地編寫代碼。

首先,懶洋洋地實例化UIActivityIndicatorView。我喜歡用一個下劃線前綴私有ivars,但這不是必需的。

-(UIActivityIndicatorView *) spinner 
{ 
    if (_spinner == nil) 
    { 
     _spinner = [[UIActivityIndicatorView alloc] initWithFrame:CGRectMake(141.0, 190.0, 80.0, 80.0)]; 
     _spinner.hidesWhenStopped = YES; 
     [self.view addSubview:_spinner]; 
    } 
    return _spinner; 
} 

現在您可以使用[self spinner]來始終引用相同的對象引用。

-(void)StartSpinner 
{ 
    [[self spinner] startAnimating]; 
} 
-(void)StopSpinner 
{ 
    [[self spinner] stopAnimating]; 
} 

現在你可以創建其他視圖控制器作爲SpinnerController子類繼承的功能。

+0

不適合我 – user3109827

1

您需要使用

+(void)StartSpinner 
{ 
    .... 
} 

和.h文件中,使公衆的方法聲明爲,

+(void)StartSpinner; 

在其他視圖控制器,導入這個類並調用方法如下:

[SpinnerController StartSpinner]; 

以及類似的其他方法。

1

U可以在SharedController

- (UIAlertView *)createProgressViewToParentView:(UIView *)view withTitle:(NSString *)title 
{ 
Alert_UserLocation = [[UIAlertView alloc] initWithTitle:@"" message:title delegate:self cancelButtonTitle:nil otherButtonTitles:nil]; 
[Alert_UserLocation show]; 

UIActivityIndicatorView *loaderView = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(130, 60, 25, 25)]; 
loaderView.tag = 3333; 
loaderView.activityIndicatorViewStyle = UIActivityIndicatorViewStyleWhiteLarge; 
loaderView.backgroundColor = [UIColor clearColor]; 
[Alert_UserLocation addSubview:loaderView]; 
[loaderView startAnimating]; 
[loaderView release]; 
return Alert_UserLocation; 
} 

- (void)hideProgressView:(UIAlertView *)inProgressView 
{ 
if(Alert_UserLocation != nil) 
{ 
    [Alert_UserLocation dismissWithClickedButtonIndex:0 animated:YES]; 
    Alert_UserLocation = nil; 
} 
} 

和進口sharedController做如下,編寫如下代碼,並在您的視圖控制器創建sharedController對象如下

sharedController = [SharedController sharedController]; 
    self.progressView = [sharedController createProgressViewToParentView:self.view withTitle:@"Loading..."]; 

你在哪裏都u必須關閉使用以下代碼

[sharedController hideProgressView:self.progressView]; 
相關問題