Sunday, October 6, 2013

UIAlertViewにテキストのinputをさせる

参考→【iOS5】UIAlertViewにsetAlertViewStyleでテキストフィールドを追加する!!

ここを参考に僕も実装しました
- (void) settingButtonTapped {
    UIAlertView *setTitle = [[UIAlertView alloc] initWithTitle:@"Set Title"
                                                       message:nil
                                                      delegate:self
                                             cancelButtonTitle:@"Cancel"
                                             otherButtonTitles:@"Save", nil];
    [setTitle setAlertViewStyle:UIAlertViewStylePlainTextInput];
    [[setTitle textFieldAtIndex:0] setText:habit.title];
    [setTitle show];
}

// Setボタンを有効にするかを決める
- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView {
    NSString *inputText = [[alertView textFieldAtIndex:0] text];
    
    if( [inputText length] >= 1 ) {
        return YES;
    } else {
        return NO;
    }
}

// アラートのsetTitleのsaveボタンが押された
-(void)alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
    NSString *input = [[alertView textFieldAtIndex:0] text];

    if (buttonIndex == 1) {
        NSLoG(@"%@", input);
    }
}

No comments:

Post a Comment