创建按钮,文本字段和插入图像编程在Xcode



我在iphone应用程序中面临意想不到的问题。无论出于什么原因,我编译这个,我得到一个SIGABRT错误。它发生在:

    textFieldRounded=[[UITextField alloc]initWithframe: CGRectMake(20,50,280,31)];
    textFieldRounded.borderStyle=UITextBorderStyleRoundedRect;
    textFieldRounded.textColor=[UIColor blackColor];
    textFieldRounded.font=[UIFont systemFontOfSize:17.0];
    textFieldRounded.placeholder=@"enter your name";
    textFieldRounded.backgroundColor=[UIColor whiteColor];
    textFieldRounded.autocorrectionType=UITextAutocorrectionTypeNo;
    textFieldRounded.backgroundColor=[UIColor clearColor];
    textFieldRounded.keyboardType=UIKeyboardTypeDefault;
    textFieldRounded.returnKeyType=UIReturnKeyDone;
    textFieldRounded.clearButtonMode=UITextFieldViewModeWhileEditing;
    [self.view addSubView:textFieldRounded];
    textFieldRounded.delegate=self;    
    UIButton *button =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [button addTarget:self action:@selector(buttonPressed:)
     forControlEvents:UIControlEventTouchDown];
    [button setTitle:@"Button" forstate:UIControlStateNormal];
    button.frame = CGRectMake(20,108,97,37);
    [self.view addSubview:button];
    label= [[UILabel alloc] initWithFrame:CGRectMake(40,243,240,21)];
    label.backgroundColor=[UIColor clearColor];
    label.textAlignment,UITextAlignmentCenter;
    label.text=@"Label";
    [self.view addSubview:label];    
    CGRect myImageRect = CGRectMake(40.0f,190.0f,240.0f,128.0f);
    image= [[UIImageView alloc] initWithFrame:myImageRect];
    [image setImage:[UIImage imageNamed:@"Default@2x.png"]];
    image.backgroundColor=[UIColor clearColor];
    image.opaque =YES;
    image.alpha=0;
    [self.view addSubview:image];
    [image release];
}

您可以创建UITextField, UIButtonUILabel如下代码:-

<<strong> UITextField strong>
UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 40)];
textField.borderStyle = UITextBorderStyleRoundedRect;
textField.font = [UIFont systemFontOfSize:15];
textField.placeholder = @"enter text";
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.keyboardType = UIKeyboardTypeDefault;
textField.returnKeyType = UIReturnKeyDone;
textField.clearButtonMode = UITextFieldViewModeWhileEditing;
textField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;    
textField.delegate = self;
[self.view addSubview:textField];
[textField release];

UIButton

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:@selector(aMethod:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:@"UIButton" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[view addSubview:button];
[button release];

UILabel

UILabel *headingLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 240, 300, 30)];
[self.view addSubview:headingLabel];

//可选

headingLabel.text = @"WELCOME";
headingLabel.textColor = [UIColor orangeColor];
headingLabel.textAlignment = UITextAlignmentCenter;
headingLabel.tag = 10;
headingLabel.backgroundColor = [UIColor clearColor];
headingLabel.font = [UIFont fontWithName:@"MarkerFelt-Thin" size:14.0];
headingLabel.hidden = NO;
headingLabel.highlighted = YES;
headingLabel.highlightedTextColor = [UIColor blueColor];
headingLabel.lineBreakMode = YES;
headingLabel.numberOfLines = 0;

UIImageView

UIImageView *myImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 150, 150)];
myImage.image = [UIImage imageNamed:@"myImage.png"];
[self.view addSubview:myImage];
[mymyImage release];

希望对你有所帮助

最新更新