LeveyPopListView多次调用

时间:2015-04-06 07:50:22

标签: ios objective-c iphone

我正在使用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方法:

- (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;
    }
}];
}

2 个答案:

答案 0 :(得分:0)

看来,当您快速点击以创建列表时,创建LeveyPopListView的方法会多次触发,从而导致创建的列表多于所需列表。

我认为您应该在列表创建方法中插入一个检查,确认如果列表已经创建,而不是创建新列表,则创建一个'。

您可以通过将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方法,将列表从超级视图中删除为 -

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

完成列表后,请调用上述方法removeLeveyPopList,这将从超级视图中删除列表。

再次当你需要创建列表调用createLeveyPopList时,它将检查列表是否已经创建,如果是,则不会创建新的其他创建列表。

答案 1 :(得分:0)

我在你的问题编辑和前一个答案的讨论后添加了另一个答案 -

假设您有一个名为CustomViewController

的班级

内部CustomViewController.h

LeveyPopListView的实例创建属性

@property (nonatomic, strong) LeveyPopListView *m_lplv;

内部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];
}

同时在同一个类(LeveyPopListView)中实现CustomViewController.m个委托 -

#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委托中您将列表实例设置为nil为self.m_lplv = nil;,这将有助于再次创建列表。

2)请记住,您无需在LeveyPopListView.hLeveyPopListView.m的任何方法中进行任何编码即可达到您的要求。除非和直到您需要更改列表创建的当前实现。