用UIAlertView绘制弹出对话框

来源:互联网 发布:mac mini 装win10 编辑:程序博客网 时间:2024/05/20 21:44

  如果想使用UIAlertView定制自己的对话框,要添加系统回调函数- (void)willPresentAlertView:(UIAlertView *)alertView,并在这个函数中加入自己想要的内容,代码如下:

 

- (void)viewDidLoad{
myalertView = [[UIAlertView alloc]initWithTitle:@"配置ip"
message:@""
delegate:self
cancelButtonTitle:@"cancel"
otherButtonTitles:@"ok",nil];
//myalertView.userInteractionEnabled = YES;

[myalertView show];

}

//adjust the appearance of alertView
- (void)willPresentAlertView:(UIAlertView *)alertView{
alertView.frame = CGRectMake(alertView.frame.origin.x, alertView.frame.origin.y-80,alertView.frame.size.width , alertView.frame.size.height+130);
//add labels
//label:ip
UILabel *label_IP = [[UILabel alloc] initWithFrame:CGRectMake(20, 30, 140, 20)];
label_IP.text = @"IP Address:";
label_IP.backgroundColor = [UIColor clearColor];
label_IP.textColor = [UIColor whiteColor];
[alertView addSubview:label_IP];
[label_IP release];
//label:getway
UILabel *label_GW = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 140, 20)];
label_GW.text = @"Geteway:";
label_GW.backgroundColor = [UIColor clearColor];
label_GW.textColor = [UIColor whiteColor];
[alertView addSubview:label_GW];
[label_GW release];
//label:subnet mask
UILabel *label_SubM = [[UILabel alloc] initWithFrame:CGRectMake(20, 130, 140, 20)];
label_SubM.text = @"Subnet Mask:";
label_SubM.backgroundColor = [UIColor clearColor];
label_SubM.textColor = [UIColor whiteColor];
[alertView addSubview:label_SubM];
[label_SubM release];
for (UIView *Outlet in [alertView subviews])
{
if(![Outlet isKindOfClass:[UITextField class]]&&![Outlet isKindOfClass:[UILabel class]])
{
UIButton* button = (UIButton*)Outlet;
button.frame = CGRectMake(button.frame.origin.x, button.frame.origin.y+130,button.frame.size.width , button.frame.size.height);
}
}
//textField:ip
textField_IP = [[UITextField alloc] initWithFrame:CGRectMake(20, 50, 240, 20)];
[alertView addSubview:textField_IP];
textField_IP.backgroundColor = [UIColor whiteColor];
//[textField_IP release];
//textField:getway
textField_GW = [[UITextField alloc] initWithFrame:CGRectMake(20, 100, 240, 20)];
[alertView addSubview:textField_GW];
textField_GW.backgroundColor = [UIColor whiteColor];
//[textField_GW setSecureTextEntry:YES];
//[textField_GW release];
//textField:subnet mask
textField_SubM = [[UITextField alloc] initWithFrame:CGRectMake(20, 150, 240, 20)];
[alertView addSubview:textField_SubM];
textField_SubM.backgroundColor = [UIColor whiteColor];
//[textField_SubM setSecureTextEntry:YES];
//[textField_SubM release];
}
//按钮响应函数
//- (void)alertView:(UIAlertView *)alertView clickdButtonAtIndex:(NSInteger)buttonIndex
- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
if ([[alertView buttonTitleAtIndex:buttonIndex] isEqualToString:@"ok"]) {
IP = textField_IP.text;
Getway = textField_GW.text;
SubnetMask = textField_SubM.text;

}

原创粉丝点击