UIButton不会隐藏,活动指示器将无法启动

时间:2015-01-06 14:31:56

标签: ios objective-c uibutton uiactivityindicatorview

我正在尝试隐藏按钮并在按下按钮时启动活动指示器,但两者都没有发生。按钮和活动都是IBOutlets。我知道按钮有效,因为它会在收到数据后推送下一个视图控制器。

以下是所有代码:

@interface MembershipInfoViewController ()

@end

@implementation MembershipInfoViewController {
    Menu *menuClass;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        menuClass = [[Menu alloc]init];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.

    self.title = @"Enter Membership Info";

    [self.loadingIndicator stopAnimating];

    self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"" style:UIBarButtonItemStylePlain target:nil action:nil];

    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
    {
        UIImage *hamburgerButton = [UIImage imageNamed:@"list_button"];
        UIBarButtonItem *listButton = [[UIBarButtonItem alloc]initWithImage:hamburgerButton style:UIBarButtonItemStylePlain target:self action:@selector(showMenuList)];
        self.navigationItem.leftBarButtonItem = listButton;

        UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(showMenuList)];
        swipeRecognizer.direction = UISwipeGestureRecognizerDirectionRight;
        [self.view addGestureRecognizer:swipeRecognizer];
    }

    UIColor *kfbBlue = [UIColor colorWithRed:8.0/255.0f green:77.0/255.0f blue:139.0/255.0f alpha:1];

    self.firstName.textColor = kfbBlue;
    self.firstName.autocorrectionType = UITextAutocorrectionTypeNo;
    self.firstName.keyboardType = UIKeyboardTypeDefault;
    self.firstName.returnKeyType = UIReturnKeyDefault;
    self.firstName.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.firstName.clearsOnBeginEditing = TRUE;
    self.firstName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    self.lastName.textColor = kfbBlue;
    self.lastName.autocorrectionType = UITextAutocorrectionTypeNo;
    self.lastName.keyboardType = UIKeyboardTypeDefault;
    self.lastName.returnKeyType = UIReturnKeyDefault;
    self.lastName.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.lastName.clearsOnBeginEditing = TRUE;
    self.lastName.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    self.membershipNumber.textColor = kfbBlue;
    self.membershipNumber.autocorrectionType = UITextAutocorrectionTypeNo;
    self.membershipNumber.keyboardType = UIKeyboardTypeDefault;
    self.membershipNumber.returnKeyType = UIReturnKeyDefault;
    self.membershipNumber.clearButtonMode = UITextFieldViewModeWhileEditing;
    self.membershipNumber.clearsOnBeginEditing = TRUE;
    self.membershipNumber.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    self.checkInfoButton.layer.borderColor = [UIColor whiteColor].CGColor;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)checkInfo {
    if ([self.firstName.text isEqual:@""]||[self.lastName.text isEqual:@""]||[self.membershipNumber.text isEqual:@""]) {
        UIAlertView *emptyFieldAlert = [[UIAlertView alloc]initWithTitle:@"All Fields Are Required!" message:@"Please fill out all fields." delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
        [emptyFieldAlert show];
    }
    else if (self.membershipNumberString.length < 10||self.membershipNumberString.length > 10) {
        UIAlertView *numberAlert = [[UIAlertView alloc]initWithTitle:@"Membership Number Must Be 10 Characters!" message:@"" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [numberAlert show];
    }
    else {
        [self.checkInfoButton setHidden:YES];
        [self.loadingIndicator startAnimating];

        NSString *urlString = @"http://127.0.0.1:8888/PhpProject1/DBTest.php?first_name=%@&last_name=%@&membership_number=%@";
        NSString *stringURL = [NSString stringWithFormat:urlString, _firstNameString, _lastNameString, _membershipNumberString];

        self.url = [NSURL URLWithString:stringURL];

        dispatch_async(kBgQueue, ^{
            NSData *data = [NSData dataWithContentsOfURL:self.url];
            if (data == nil)
            {
                NSLog(@"data is nil");
            }
            else
            {
                [self performSelectorOnMainThread:@selector(fetchData:) withObject:data waitUntilDone:YES];
            }

            if ([self.responseStatus isEqual:@"A"]) {
                MembershipCardViewController *cardView = [[MembershipCardViewController alloc]initWithNibName:nil bundle:nil];
                NSString *fullName = [[self.responseFirstName stringByAppendingString:@" "]stringByAppendingString:self.responseLastName];
                cardView.nameString = fullName;
                [self.navigationController pushViewController:cardView animated:YES];
            }
            else {
                UIAlertView *notActiveAlert = [[UIAlertView alloc]initWithTitle:@"" message:@"There is no active membership matching this information. If you are a past member, please make sure your membership is paid for the current year." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:@"My Account", nil];
                [notActiveAlert show];
            }
        });
    }
}

- (void)fetchData:(NSData *)responseData {
    NSError *error;

    NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error];

    for (int i = 0; i < jsonArray.count; i++) {
        NSDictionary *jsonElement = jsonArray[i];

        self.responseStatus = jsonElement[@"STATUS"];
        self.responseFirstName = jsonElement[@"FIRST_NAME"];
        self.responseLastName = jsonElement[@"LAST_NAME"];
        self.responseNumber = jsonElement[@"MEMBERSHIP_NUMBER"];
    }

    [self.loadingIndicator stopAnimating];
    [self.checkInfoButton setHidden:NO];
}

- (void) myKYFB {
    KFBAppDelegate *appDelegate = (KFBAppDelegate *)[[UIApplication sharedApplication]delegate];
    KFBNavControllerViewController *navController;

    MyKYFB *myKYFB = [[MyKYFB alloc]initWithNibName:nil bundle:nil];
    navController = [[KFBNavControllerViewController alloc]initWithRootViewController:myKYFB];
    [UIView transitionWithView:appDelegate.window
                      duration:0.5
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        appDelegate.window.rootViewController = navController;
                    }
                    completion:nil];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    if (buttonIndex == 1) {
        [self myKYFB];
    }
}

- (void)showMenuList {
    [menuClass createMenu];
}

- (void)closeMenu {
    [menuClass removeMenu];
}

#pragma mark - TextField Delegate Methods

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.firstName resignFirstResponder];
    [self.lastName resignFirstResponder];
    [self.membershipNumber resignFirstResponder];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.firstName resignFirstResponder];
    [self.lastName resignFirstResponder];
    [self.membershipNumber resignFirstResponder];

    return YES;
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    self.firstNameString = self.firstName.text;
    self.lastNameString = self.lastName.text;
    self.membershipNumberString = self.membershipNumber.text;
}


@end

enter image description here

2 个答案:

答案 0 :(得分:0)

您的代码应该有效,但有时这段代码不起作用。许多用户以前也面临这个问题,使用下面的代码,这肯定会起作用。

dispatch_async(dispatch_get_main_queue(), ^{
    self.checkInfoButton.hidden = YES; //works
});

答案 1 :(得分:0)

我必须在推送新的视图控制器之前将其从fetchData:移动到右边。

[self.loadingIndicator stopAnimating];
[self.checkInfoButton setHidden:NO];