2015-04-06 86 views
0

我正在使用LeveyPopListView我改變了LeveyPopListView的大小以防止多次調用LeveyPopListView。但在我的應用程序,當我點擊太快顯示另一個LeveyPopListView重疊第一個LeveyPopListView。查看圖片以供參考(LeveyPopListView背景較暗,因爲有兩個重疊彈出)。LeveyPopListView多次調用

enter image description here

(主要頁面調用LeveyPopListView一) 創建了一個方法,用於創建LeveyPopListView

- (void) createLeveyPopList 
{ 
NSInteger numberOfJobs = [[[[[self.FilterDictionary objectForKey:@"sub_slots"] valueForKey:@"company_group"] valueForKey:@"job_count"] objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row] intValue]; 
NSString *jobs_name = xapp.jobName; 
NSString *company_name; 

if(numberOfJobs > 1) 
{ 
    isToDetail = false; 
    NSString *company_id = [[[[self.FilterDictionary objectForKey:@"sub_slots"] valueForKey:@"company_group"] valueForKey:@"company_id"] objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row]; 
    company_name = [[[[self.FilterDictionary objectForKey:@"sub_slots"] valueForKey:@"company_group"] valueForKey:@"company_name"] objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row]; 
    NSDictionary *specificCompany = [NSDictionary dictionaryWithObjectsAndKeys:company_id,@"company_id", nil]; 

    if(specificCompany.count>0) 
    { 
     NSError *error; 
     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:specificCompany 
                  options:0 // Pass 0 if you don't care about the readability of the generated string 
                  error:&error]; 

     if (! jsonData) 
     { 
      NSLog(@"Got an error: %@", error); 
     } 
     else 
     { 
      strJsonStringFilter = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
     } 
    } 

    allJobsDictionary = [NSJSONSerialization JSONObjectWithData:[network getData:[NSString stringWithFormat:@"get_all_job_offers?pt_id=%@&filter=%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"pt_id"], strJsonStringFilter]] options:kNilOptions error:nil]; 
    jobsToDisplay=(NSArray*)[allJobsDictionary objectForKey:@"sub_slots"]; 
} 

if(self.lplv != nil) 
    return; 

self.lplv = [[LeveyPopListView alloc] initWithTitle:company_name options:jobsToDisplay jobName:jobs_name handler:^(NSInteger anIndex){ 
}]; 

self.lplv.delegate = self; 
[self.lplv showInView:self.view animated:YES]; 
} 

(位於LeveyPopListView類)爲我的關閉按鈕 代碼:

_close = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
     [_close addTarget:self 
        action:@selector(fadeOut) 
     forControlEvents:UIControlEventTouchUpInside]; 

(位於LeveyPopListView類) FADEOUT會見HOD:

- (void)fadeOut { 
[UIView animateWithDuration:.35 animations:^{ 
    self.transform = CGAffineTransformMakeScale(1.3, 1.3); 
    self.alpha = 0.0; 
} completion:^(BOOL finished) { 
    NSLog(@"FINISH"); 
    if (finished) { 
     [[NSNotificationCenter defaultCenter] removeObserver:self]; 
     self.lplv.delegate = nil; 
     [self.lplv removeFromSuperview]; 
     self.lplv = nil; 
    } 
}]; 
} 

回答

0

看來,該方法在其上創建,當你點擊快速創建列表中LeveyPopListView觸發一次以上,導致創建比需要更多的名單。

我認爲你應該在列表創建方法中插入一個檢查,確認'如果列表已經被創建,而不是創建新的或者創建一個'。

@property (nonatomic, strong) LeveyPopListView *m_lplv; 

內創建列表

- (void) createLeveyPopList 
{ 
    if(self.m_lplv != nil) 
     return; 

    self.m_lplv = [[LeveyPopListView alloc] initWithTitle:@"Share Photo to..." options:_options]; 
    self.m_lplv.delegate = self; 
    [self.m_lplv showInView:self.window animated:YES]; 
    [self.m_lplv release]; 
} 

還要創建一個remove方法從超視圖中刪除列表, -

可以通過聲明的LeveyPopListView的實例作爲成員變量,這樣做 -

- (void) removeLeveyPopList 
{ 
    self.m_lplv.delegate = nil; 
    [self.m_lplv removeFromSuperview]; 
} 

調用上述方法removeLeveyPopList,當你完成列表,這將從超級視圖中移除列表。

再次當你需要創建列表調用createLeveyPopList這將檢查列表是否已經被創建,如果是,那麼它不會創建新的其他創建一個。

+0

這是我做的,我在shouldPerformSegueWithIdentifier中調用了createLeveyPopList。現在當我點擊關閉按鈕並調用crateLeveyPopList時,LeveyPopList不會被創建。這是因爲代碼m.lplv不是空的。我應該在哪裏調用removeLeveyPopList?請參閱關於按鈕方法的更新。 – aianLee 2015-04-06 10:33:53

+0

更改[self removeFromSuperview];到[self.lplv removeFromSuperview]; (void)fadeOut {UIView animateWithDuration:.35動畫:{self.transform = CGAffineTransformMakeScale(1.3,1.3);} {self.transform = CGAffineTransformMakeScale(1.3,1.3); self.alpha = 0.0; }完成:^(BOOL完成){ NSLog(@「FINISH」); if(finished){[NSNotificationCenter defaultCenter] removeObserver:self]; [self.lplv removeFromSuperview]; } }]; } – 2015-04-06 10:38:00

+0

hmm .. fadeOut方法在leveyPopListView類中被調用和創建..我改變了[self removeFromSuperview];到[self.lplv removeFromSuperview];仍然得到了相同的結果。 – aianLee 2015-04-06 10:50:30

0

我加入了以下的問題和討論以前的答案做的修改另一個答案 -

假設你有一個名爲CustomViewController

CustomViewController.h

爲實例創建性能等級LeveyPopListView as

@property (nonatomic, strong) LeveyPopListView *m_lplv; 

Inside CustomViewController.m

創建列表, -

- (void) createLeveyPopList 
{ 
NSInteger numberOfJobs = [[[[[self.FilterDictionary objectForKey:@"sub_slots"] valueForKey:@"company_group"] valueForKey:@"job_count"] objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row] intValue]; 
NSString *jobs_name = xapp.jobName; 
NSString *company_name; 

if(numberOfJobs > 1) 
{ 
    isToDetail = false; 
    NSString *company_id = [[[[self.FilterDictionary objectForKey:@"sub_slots"] valueForKey:@"company_group"] valueForKey:@"company_id"] objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row]; 
    company_name = [[[[self.FilterDictionary objectForKey:@"sub_slots"] valueForKey:@"company_group"] valueForKey:@"company_name"] objectAtIndex:[self.jobsTableView indexPathForSelectedRow].row]; 
    NSDictionary *specificCompany = [NSDictionary dictionaryWithObjectsAndKeys:company_id,@"company_id", nil]; 

    if(specificCompany.count>0) 
    { 
     NSError *error; 
     NSData *jsonData = [NSJSONSerialization dataWithJSONObject:specificCompany 
                  options:0 // Pass 0 if you don't care about the readability of the generated string 
                  error:&error]; 

     if (! jsonData) 
     { 
      NSLog(@"Got an error: %@", error); 
     } 
     else 
     { 
      strJsonStringFilter = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding]; 
     } 
    } 

    allJobsDictionary = [NSJSONSerialization JSONObjectWithData:[network getData:[NSString stringWithFormat:@"get_all_job_offers?pt_id=%@&filter=%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"pt_id"], strJsonStringFilter]] options:kNilOptions error:nil]; 
    jobsToDisplay=(NSArray*)[allJobsDictionary objectForKey:@"sub_slots"]; 
} 

if(self.m_lplv != nil) 
    return; 

self.m_lplv = [[LeveyPopListView alloc] initWithTitle:company_name options:jobsToDisplay jobName:jobs_name handler:^(NSInteger anIndex){ 
}]; 

self.m_lplv.delegate = self; 
[self.m_lplv showInView:self.view animated:YES]; 
} 

也實現了在同一類(CustomViewController.m)的LeveyPopListView代表原樣

#pragma mark - LeveyPopListView delegates 
- (void)leveyPopListView:(LeveyPopListView *)popListView didSelectedIndex:(NSInteger)anIndex 
{ 
    // selected an item from popListView at anIndex 
    // code to be executed on selecting any item from the list 
} 
- (void)leveyPopListViewDidCancel 
{ 
    // called when the cross button of the list is tapped 
    self.m_lplv = nil 
} 

注意

1)按照以上實現的細節,你可以解決點擊快速,也在英寸時多列表創建的問題leveyPopListViewDidCancel代表您將設置列表實例爲零作爲self.m_lplv = nil;,這將有助於再次創建列表。 2)請記住,您不需要在LeveyPopListView.hLeveyPopListView.m的任何方法中進行任何編碼以達到您的要求。除非和直到你需要改變列表創建的當前實現。