UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"Error" message:@"I'm sorry Dave, I'm afraid I can't do that." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil] autorelease];
[alert setTag:12];
[alert show];
... later ...
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 12) { // it's the Error alert
if (buttonIndex == 0) { // and they clicked OK.
// do stuff
}
}
}
[alert setTag:12];
[alert show];
... later ...
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
if ([alertView tag] == 12) { // it's the Error alert
if (buttonIndex == 0) { // and they clicked OK.
// do stuff
}
}
}
自定义UIAlertView
- (void)willPresentAlertView:(UIAlertView *)alertView {
for( UIView * view in alertView.subviews )
{ if( [view isKindOfClass:[UILabel class]] )
{
UILabel* label = (UILabel*) view;
label.textAlignment = UITextAlignmentLeft;
}
}
}//文本居左显示
添加控件,设置tag可以多个alertview同时使用
- (void)willPresentAlertView:(UIAlertView *)alertView{CGRect frame = alertView.frame;if( alertView==twitterAlertView ){frame.origin.y -= 120;frame.size.height += 80;alertView.frame = frame;for( UIView * view in alertView.subviews ){if( ![view isKindOfClass:[UILabel class]] ){CGRect btnFrame = view.frame;btnFrame.origin.y += 70;view.frame = btnFrame;}}UITextField* accoutName = [[HelperClass createTextField] autorelease];//这里创建一个UITextField对象UITextField* accoutPassword = [[HelperClass createTextField] autorelease];//这里创建一个UITextField对象accoutName.frame = CGRectMake( 10, 40,frame.size.width - 20, 30 );accoutPassword.frame = CGRectMake( 10, 80,frame.size.width -20, 30 );accoutName.placeholder = @"Account Name";accoutPassword.placeholder = @"Password";accoutPassword.secureTextEntry = YES;[alertView addSubview:accoutPassword];[alertView addSubview:accoutName];}}UIActionSheet同理实现- (void)willPresentActionSheet:(UIActionSheet *)actionSheet 即可