2013-01-17 151 views
1

我有一個FirstView加載了tableView,當點擊tableviewCell的附件按鈕時,它會去SecondView.TheView中的數據從服務url加載,所以它需要時間。所以我需要顯示activityIndi​​cator時accessorybutton被輕敲,加載整個數據後停止它.Bt我無法看到activityindicator顯示。UIActivityIndi​​cator not displayed

這是工作的代碼林:

- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 

activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)]; 
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; 
activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray]; 
activityIndicator.color = [UIColor redColor]; 
[scrollView addSubView:activityIndicator]; 
[activityIndicator startAnimating]; 
activityIndicator.frame=CGRectMake(140, 195, 37, 37); 
[activityIndicator startAnimating]; 

     SecondView *secView = [[SecondView alloc] initWithNibName:@"SecondView" bundle:nil]; 
     secView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal; 

     UITableViewCell* cell = [tableView cellForRowAtIndexPath:indexPath]; 
     UILabel *name1 = (UILabel*)[cell viewWithTag:111]; 
     secView.provName = [name1 text]; 

     [self presentModalViewController: secView animated:NO]; 

     [secView release]; 
    } 

我在哪裏要去錯了嗎?

編輯:

SecondView:

-(void)viewWillAppear:(BOOL)animated 

{ 
    activityIndictr = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)]; 
    activityIndictr.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; 

    activityIndictr.color = [UIColor redColor]; 
    [testscroll addSubview:activityIndictr]; 
    [activityIndictr startAnimating]; 

} 

-(void)viewDidLoad 
{ 
    [super viewDidLoad] 
    // I m calling many service urls(10) and assigning to labels and tableview 
    [activityIndictr stopAnimating]; 

} 

回答

0

您分配活動指標,拋出的第一個客場,然後開始動畫的第二個兩次,絕不添加任何活動的指標爲任何現有的視圖。難怪它沒有出現。嘗試[[secView view] addSubview:activityIndicator]或類似的東西。

+0

很抱歉,但我不明白這u.By stmnt「拋出的第一個客場」如果不使用第一個指標則Y來創建它。? – Sindhia

1

您應該調用SecondView的ViewWillAppear中的活動指示器,以便在推入此視圖後立即啓動活動指示器,然後在從url解析所有數據時將其解除。

這是因爲第二個視圖控制器就會立即推數據需要一定的時間來加載

+0

+1這就是問題.. – Rushi

+0

與你一起走了,但我沒有看到指示器在我點擊accesssorybutton後立即顯示,並在SecondView完全加載後出現 – Sindhia

+0

@Sindhia第一個視圖進入第二個視圖需要多長時間? – IronManGill

0

在SecondView的viewWillAppear中做到這一點:

activityIndicator = [[UIActivityIndicatorView alloc]initWithFrame:CGRectMake(143, 220, 37, 37)]; 
activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray; 
[self.view addSubview:activityIndicator]; 
[activityIndicator startAnimating]; 
+0

試過我們的代碼,但我沒有看到指示器後,我點擊accesssorybutton並出現在SecondView完全加載後立即顯示。任何東西我應該需要修改? – Sindhia

+0

我編輯我的代碼試試這個。 – Rushi

+0

仍然像prevoius one.Dont現在y相同的結果y – Sindhia

0

我再次更新了答案。

把這個代碼的appdelegate而在的appdelegate定義靜態的物體,像下面

靜態UIActivityIndi​​catorView * activityView;

+(void)showActivityIndicator{ 

if (!activityView) { 
    activityView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge]; 
    activityView.center = CGPointMake(160, 230); 
    [activityView startAnimating]; 
    UIWindow *win = [UIApplication sharedApplication].keyWindow; 
    [win addSubview:activityView]; 
} 
} 


+(void)removeActivityIndicator{ 
if ([activityView superview]) { 
    [activityView removeFromSuperview]; 
    activityView = nil; 
} 
} 






- (void) tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath 
{ 
UIWindow *window = [UIApplication sharedApplication].keyWindow; 

//global static activity indicator... you can access from any other view controlller 

[AppDelegate showActivityIndicator]; 

} 

並得到響應後

[AppDelegate removeActivityIndicator]; 
+0

我做到了,因爲你說過了。在AppDelegate中創建activtyindicator,並在accesssorybutton tapped中調用它。但它不工作。當我在accessorybuttonTapped(FirstView)中將窗口添加到窗口並在加載數據後從SecondView中刪除它時,我得到錯誤「activityIndi​​cator undeclared.I我應該創建它嗎? – Sindhia

+0

@Sindhia我已更新代碼..找到它..如果您有任何麻煩讓我知道 – SachinVsSachin

+0

不,它不工作Sachin.It要showActivityIndi​​cator (),但執行裏面,但immediatley中的語句,因爲它返回到SecondView * secView = [[SecondView alloc] initWithNibName:@「SecondView」bundle:nil];指示器不被識別。可以使SecondView * secView = [[ SecondView alloc] initWithNibName:@「SecondView」bundle:nil]開始2秒後activityindicator開始動畫? – Sindhia