我有一個視圖控制器(VC)有3個按鈕(國家,州,市)。點擊這些按鈕後,我將展示另一個VC(使用popover segue),這是一個搜索表來搜索國家(或州或城市)。現在,如果用戶單擊兩個按鈕,則同時顯示兩個視圖控制器。我只想要一個人。如何做到這一點?禁用多點觸摸不工作ios
嘗試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;
}
任何想法?
感謝,
請分享一些可以幫助你的代碼。 :) – Rashad
self.view.multipleTouchEnabled = NO不應該工作,因爲每個按鈕是一個差異視圖。 –
觸摸後您的按鈕是否可用?或者用戶是否必須同時打兩個按鈕?爲什麼這是一個問題?有一件事你可以嘗試,就是讓一個方法處理所有三個按鈕,然後在運行時設置一個標誌。如果設置了該標誌,代碼將不會執行,因此如果該方法被調用兩次,代碼只運行一次。當你返回到vc時(例如,在結果視圖中調用的協議方法中),你重置標誌 – Moonwalkr