禁用多点触控不工作ios

时间:2014-03-03 09:39:33

标签: ios iphone objective-c

我有一个视图控制器(VC)有3个按钮(国家,州,城市)。点击这些按钮,我将呈现另一个VC(使用popover segue),这是一个搜索国家(或州或城市)的搜索表。现在,如果用户单击两个按钮,则两个视图控制器同时显示。我只想要一个人。如何做到这一点?

尝试过self.view.multipleTouchEnabled = NO;但没有工作。 searchVC的代码:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIImage *bgApplication = [UIImage imageNamed:@"bg-app-568h.jpg"];
    self.view.backgroundColor = [UIColor colorWithPatternImage:bgApplication];

    self.searchItems = [self loadData];
    self.filteredSearchItems = [NSMutableArray arrayWithCapacity:[self.searchItems count]];

    for (UIView *view in self.SearchBarBase.subviews){
        if ([view isKindOfClass: [UITextField class]]) {
            UITextField *tf = (UITextField *)view;
            tf.delegate = self;
            break;
        }
    }
}

- (NSArray *)loadData
{
    MatchDayDataController * sharedController = [MatchDayDataController sharedDataController];
    NSArray *data = [sharedController fetchStates];
    //NSLog(@"states: %@", data);

    return data;
}

演示者VC的代码:

-(void) viewWillAppear:(BOOL)animated
{
    keyboardIsShown = NO;

    MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];

    // Load match venue data
    self.locationField.text = sharedController.matchVenue;

    //Load home team related data
    NSString *stateText = [sharedController.homeStateName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([stateText length] == 0 )
    {
        self.selectHomeAssoc.enabled = NO;
        self.selectHomeClub.enabled = NO;
    }
    NSString *homeAssocText = [sharedController.homeAssociationName stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([homeAssocText length] == 0 )
    {
        self.selectHomeAssoc.enabled = YES;
        self.selectHomeClub.enabled = NO;
    }
    NSString *homeClubText = [self.homeClub.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if ([homeClubText length] == 0 )
    {
        self.selectHomeClub.enabled = YES;
    }

    self.stateTextFieldHome.text = sharedController.homeStateName;
    self.homeAssociation.text = sharedController.homeAssociationName;
    self.homeClub.text = sharedController.homeClubName;;
    self.homeTeam.text = sharedController.homeTeamName;

    // Away team related data
    self.stateTextFieldAway.text = sharedController.awayStateName;
    self.awayAssociation.text = sharedController.awayAssocationName;
    self.awayClub.text = sharedController.awayClubName;
    self.awayTeam.text = sharedController.awayTeamName;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillShowNotification
                                                  object:nil];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:UIKeyboardWillHideNotification
                                                  object:nil];

    MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];

    sharedController.matchVenue = self.locationField.text;

    // Save Venue related data
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"dd/MM/yyyy HH:mm"];
    NSString *dateTimeString = [self.dateField.text stringByAppendingString:@" "];

    dateTimeString = [dateTimeString stringByAppendingString:self.timeField.text];
    //NSLog(@"DateTimeString : %@", dateTimeString);
    NSDate *matchDateTime = [dateFormatter dateFromString:dateTimeString];
    sharedController.inspectionDate = matchDateTime;

    //Save Home team related data
    sharedController.homeTeamName = self.homeTeam.text;
    // Save away team related data
    sharedController.awayTeamName = self.awayTeam.text;
    [sharedController saveData];

    // hide the keyboard when we come back after leaving the cursor on text field.
    // I have called resignFirstResponder on homeTeam Text field. You can use any of text field to hide.
    [self.homeTeam resignFirstResponder];
    [self.awayTeam resignFirstResponder];
}

- (void)viewDidDisappear:(BOOL)animated
{
    [self.view endEditing:YES];
    [super viewDidDisappear:animated];
}


// This event is called when the user clicks on Done/Next button in the key board.
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return NO;
}

-(BOOL)shouldPerformSegueWithIdentifier:(NSString *)segue sender:(id)sender
{
    if (![sender isKindOfClass:[UIBarButtonItem class]]) {
        return true;
    }

    return [JLTValidator validateFields:@[self.locationField, self.dateField, self.timeField, self.homeTeam, self.homeClub, self.homeAssociation, self.stateTextFieldHome, self.stateTextFieldAway, self.awayAssociation, self.awayClub, self.awayTeam]];
}

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    //NSLog(@"segue.identifier : %@", segue.identifier);
    self.popSegue = (UIStoryboardPopoverSegue *)segue;
    if([segue.identifier isEqualToString:@"toSearchHomeStateFromButton"]
       || [segue.identifier isEqualToString:@"toSearchHomeStateFromText"] )
    {
        SearchHomeStateViewController *viewController = segue.destinationViewController;
        viewController.delegate = (id) self;
    }
    else if ([segue.identifier isEqualToString:@"toSearchHomeAssocFromButton"] || [segue.identifier isEqualToString:@"toSearchHomeAssocFromText"])
    {
        HomeAssocSearchViewController *viewController = segue.destinationViewController;
        viewController.delegate = (id) self;
    }
    else if ([segue.identifier isEqualToString:@"toSearchHomeClubFromButton"] || [segue.identifier isEqualToString:@"toSearchHomeClubFromText"])
    {
        SearchHomeClubViewController *viewController = segue.destinationViewController;
        viewController.delegate = (id) self;
    }
    else if([segue.identifier isEqualToString:@"toSearchAwayStateFromButton"] || [segue.identifier isEqualToString:@"toSearchAwayStateFromText"])
    {
        StateViewController *viewController = segue.destinationViewController;
        viewController.delegate = (id) self;
    }
    else if ([segue.identifier isEqualToString:@"toSearchAwayAssocFromButton"] || [segue.identifier isEqualToString:@"toSearchAwayAssocFromText"])
    {
        SearchAwayAssocViewController *viewController = segue.destinationViewController;
        viewController.delegate = (id) self;
    }
    else if ([segue.identifier isEqualToString:@"toSearchAwayClubFromButton"] || [segue.identifier isEqualToString:@"toSearchAwayClubFromText"])
    {
        SearchAwayClubViewController *viewController = segue.destinationViewController;
        viewController.delegate = (id) self;
    }
}

-(void) searchHomeStateDone:(NSString *)selectedState
{
    MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
    sharedController.stateAwayTeam = sharedController.stateHomeTeam;
    sharedController.homeTeamName = EMPTY_STRING;
    sharedController.awayStateName = sharedController.homeStateName;
    sharedController.awayAssocationName = EMPTY_STRING;
    sharedController.awayAssociationId = EMPTY_STRING;
    sharedController.awayClubName = EMPTY_STRING;
    sharedController.awayClubId = EMPTY_STRING;

    [self updateDataOnScreen];
    self.selectHomeAssoc.enabled = YES;
    self.selectHomeClub.enabled = NO;
    [self.popSegue.popoverController dismissPopoverAnimated:YES];
}

-(void) searchHomeAssocDone:(NSString *)selectedHomeAssoc
{
    MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
    sharedController.awayAssocationName = sharedController.homeAssociationName;
    sharedController.homeTeamName = EMPTY_STRING;
    sharedController.awayStateName = sharedController.homeStateName;
    sharedController.awayAssociationId = sharedController.homeAssociationId;
    sharedController.awayClubName = EMPTY_STRING;
    sharedController.awayClubId = EMPTY_STRING;

    [self updateDataOnScreen];
    self.selectHomeClub.enabled = YES;
    [JLTValidator clearTextFieldValidation:self.homeAssociation];
    [self.popSegue.popoverController dismissPopoverAnimated:YES];
}

-(void) searchHomeClubDone:(NSString *)selectedHomeClub
{
    MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
    sharedController.homeTeamName = EMPTY_STRING;

    [self updateDataOnScreen];
    [JLTValidator clearTextFieldValidation:self.homeClub];
    [self.popSegue.popoverController dismissPopoverAnimated:YES];

}

-(void) searchAwayStateDone:(NSString *)selectedAwayState
{
    MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
    sharedController.homeTeamName = self.homeTeam.text;

    [self updateDataOnScreen];
    [JLTValidator clearTextFieldValidation:self.stateTextFieldAway];
    [self.popSegue.popoverController dismissPopoverAnimated:YES];
}

-(void) searchAwayAssocDone:(NSString *)selectedAwayAssoc
{
    MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
    sharedController.homeTeamName = self.homeTeam.text;

    [self updateDataOnScreen];
    [JLTValidator clearTextFieldValidation:_awayAssociation];
    [self.popSegue.popoverController dismissPopoverAnimated:YES];
}

-(void) searchAwayClubDone:(NSString *) selectedAwayClub;
{
    MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];
    sharedController.homeTeamName = self.homeTeam.text;

    [self updateDataOnScreen];
    [JLTValidator clearTextFieldValidation:_awayClub];
    [self.popSegue.popoverController dismissPopoverAnimated:YES];
}

-(void) updateDataOnScreen
{
    MatchDayDataController *sharedController = [MatchDayDataController sharedDataController];

   // sharedController.awayTeamName = EMPTY_STRING;

    self.stateTextFieldHome.text = sharedController.homeStateName;
    self.homeAssociation.text =sharedController.homeAssociationName;
    self.homeClub.text = sharedController.homeClubName;
    self.homeTeam.text = sharedController.homeTeamName;
    self.stateTextFieldAway.text = sharedController.awayStateName;
    self.awayAssociation.text = sharedController.awayAssocationName;
    self.awayClub.text = sharedController.awayClubName;
    //self.awayTeam.text = sharedController.awayTeamName;
}

有什么想法吗?

谢谢,

2 个答案:

答案 0 :(得分:1)

您可以创建静态bool变量busy,并在每个按钮操作方法的第一行检出此变量的状态。如果busy等于NO,则将她设置为YES,并在最后一行操作方法或其他位置将她设置为NO。如果busy等于YES,则会从操作方法返回。因此,直到第一个被调用的方法没有完成,其他方法才会运行。

@implementation yourController 

-(void)changeCountry
{
    if (!busy)
    {
        busy = YES;
        // do what you need
    } else return;
}

-(void)changeState
{
    if (!busy)
    {
        busy = YES;
        // do what you need
    } else return;
}

@end

你应该实现singleton,它返回一个具有BOOL值的静态实例,以便在不同的viewControllers(1 mainC + 3 popOverC)中使用他,并在完成编辑后将busy设置为NO

答案 1 :(得分:0)

尝试self.view.exclusiveTouch = YES;