2010-08-04 27 views
5

Tôi đang cố gắng thêm uitextfield vào số alterview của mình. Khi người dùng cố gắng nhập văn bản, alterview sẽ chuyển lên một chút để bàn phím không trùng lặp và khi nhấn phím đã hoàn thành, bàn phím sẽ biến mất và alertview sẽ chuyển trở lại.iphone-sdk: Thêm trường văn bản vào UIAlertview không hoạt động trong iOS 4?

Tất cả đều hoạt động tốt khi chạy trong iOS 3.1.2 (và cũng trong 3.2) nhưng ngay sau khi tôi thử chạy dưới iOS 4, alertview được hiển thị ở vị trí sai và bàn phím sẽ không biến mất. Bất kỳ đề xuất? Đây là mã của tôi:

- (void)addItemAction{ 

workoutName = [[UIAlertView alloc] initWithTitle:@"New Workout" message:@"Insert the name of your new workout:\n    " delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"Done", nil]; 
workoutName.cancelButtonIndex = 0; 
UITextField *titleField = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 90.0, 260.0, 25.0)]; 
titleField.delegate = self; 
titleField.borderStyle = UITextBorderStyleRoundedRect; 
titleField.returnKeyType = UIReturnKeyDone; 
[workoutName addSubview:titleField]; 
[workoutName show]; 


} 
- (BOOL)textFieldShouldReturn:(UITextField *)textField { 


[textField resignFirstResponder]; 
return YES; 

} 

- (void)textFieldDidBeginEditing:(UITextField *)textField { 

[UIView beginAnimations:nil context:NULL]; 
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, -70.0); 
[workoutName setTransform:myTransform]; 
[UIView commitAnimations]; 

} 

- (void)textFieldDidEndEditing:(UITextField *)textField { 

[UIView beginAnimations:nil context:NULL]; 
CGAffineTransform myTransform = CGAffineTransformMakeTranslation(0.0, 0.0); 
[workoutName setTransform:myTransform]; 
[UIView commitAnimations]; 
self.newWorkout = textField.text; 

} 

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex{ 

if (buttonIndex == 1) { 
    if (self.newWorkout != @"TestWorkout"){ 
    [self.workoutPlanArray insertObject:[NSDictionary dictionaryWithObjectsAndKeys:self.newWorkout, @"titleValue", @"04.08.10", @"dateValue", nil] atIndex:counter]; 
    counter++; 
    [self.tableView reloadData]; 
    } 
} 


} 

Trả lời

5

Tôi cũng đang sử dụng chế độ xem cảnh báo với một trường văn bản trong ứng dụng của mình, & cũng vậy trên ioS 4.0. Làm việc tốt.

Đây là đoạn mã mẫu: _

-(void)showAlert{ 

showAlert=YES; //boolean variable 

createNewAlert =[[UIAlertView alloc] initWithTitle:@"ADD item" message:@"Put it blank textfield will cover this" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:@"OK", nil]; 

UITextField *txtName = [[UITextField alloc] initWithFrame:CGRectMake(12.0, 45.0, 260.0, 25.0)]; 
[email protected]""; 
[txtName setBackgroundColor:[UIColor whiteColor]]; 
[txtName setKeyboardAppearance:UIKeyboardAppearanceAlert]; 
[txtName setAutocorrectionType:UITextAutocorrectionTypeNo]; 

[txtName setTextAlignment:UITextAlignmentCenter]; 
[createNewAlert addSubview:txtName]; 


[createNewAlert show]; 
} 

Ngoài ra bạn có thể tham khảo hai phương pháp sau đây được gọi là ngày thông báo bàn phím.

-(void)keyboardWillShow: (id) notification { 

if(showAlert==YES) 
{ 

    [UIView beginAnimations:nil context:NULL]; 
    [UIView setAnimationDuration:0.3]; 
    [createNewAlert setTransform:CGAffineTransformMakeTranslation(0,-60)]; 
    [createNewAlert show]; 
    [UIView commitAnimations]; 
} 

}

-(void)keyboardWillHide: (id) notification { 

if(showAlert==YES) 
{ 


     [UIView beginAnimations:nil context:NULL]; 
     [UIView setAnimationDuration:0.3]; 
     [createNewAlert setTransform:CGAffineTransformMakeTranslation(0,+60)]; 

     [UIView commitAnimations]; 

} 
} 
0

tôi không có đầu mối (chưa) liên quan đến vị trí của các tỉnh táo, nhưng bạn có thể thử gửi resignFirstResponder trong việc thực hiện textFieldShouldReturn của bạn vào alertView.

Ngoài ra, điều đó sẽ làm cho bàn phím biến mất (ngay cả khi tôi không biết chính xác lý do tại sao).

1

Tôi cũng gặp sự cố này trong iOS 4. Tôi đã cố gắng làm một bản dịch trên nó và nhận ra rằng nó không quan trọng bản dịch, chỉ có một bản dịch được gọi.

myAlert = my AlertView 
CGAffineTransform rotate = CGAffineTransformMakeTranslation(0.0f, 0.0f); 
myAlert.transform = rotate; 
[myAlert show]; 
[myAlert release]; 

Phải có một cuộc gọi lại được kích hoạt từ bản dịch nhưng điều này khắc phục được sự cố. Nó sẽ hoạt động như trong phiên bản trước iOS 4.0.

0

Mở tệp xib, nhấp vào UIView và đảm bảo rằng hộp kiểm "User Interaction Enabled" được chọn.

Các vấn đề liên quan