按钮操作不起作用

时间:2016-06-23 09:30:25

标签: ios objective-c

    UITextField *tf2 = [[UITextField alloc] initWithFrame:CGRectMake(10, tf1.frame.origin.y+55, 200, 30)];
    tf2.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
    //tf.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
    tf2.backgroundColor=[UIColor whiteColor];
    tf2.borderStyle=UITextBorderStyleLine;
    tf2.placeholder=@"Capture Invoice";
    tf2.textAlignment=UITextAlignmentCenter;


    UIButton *invoiceBtn=[[UIButton alloc]initWithFrame:CGRectMake(250, tf1.frame.origin.y+55,50,30)];

    UIImage *buttonImage = [UIImage imageNamed:@"camera-app-icon_1x.png"];
    [invoiceBtn setImage:buttonImage forState:UIControlStateNormal];
    [invoiceBtn addTarget:self action:@selector(invoiceClicked:) forControlEvents:UIControlEventTouchUpInside];

   view1 = [[UIView alloc] initWithFrame:CGRectMake(0,270, 100, 40)];

   [view1 addSubview:tf2];
   [view1 addSubview:invoiceBtn];

3 个答案:

答案 0 :(得分:1)

首先增加view1的框架尺寸,然后将此行添加到代码中

 [self.view addSubView: view1];
像这样:

UITextField *tf2 = [[UITextField alloc] initWithFrame:CGRectMake(10, 55, 200, 30)];
    tf2.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
    //[tf2 setPlaceholder:@"hello"];
    //tf.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
    tf2.backgroundColor=[UIColor whiteColor];
    tf2.borderStyle=UITextBorderStyleLine;
    tf2.placeholder=@"Capture Invoice";
    tf2.textAlignment=UITextAlignmentCenter;


    UIButton *invoiceBtn=[[UIButton alloc]initWithFrame:CGRectMake(25, 100,50,30)];

    //UIImage *buttonImage = [UIImage imageNamed:@"camera-app-icon_1x.png"];
    [invoiceBtn setBackgroundColor:[UIColor blueColor]];
    [invoiceBtn setTitle:@"Click" forState:UIControlStateNormal];
    //[invoiceBtn setImage:buttonImage forState:UIControlStateNormal];
    [invoiceBtn addTarget:self action:@selector(invoiceClicked:) forControlEvents:UIControlEventTouchUpInside];

    UIView* view1 = [[UIView alloc] initWithFrame:CGRectMake(0,170, 300, 400)];
    [view1 setBackgroundColor:[UIColor grayColor]];
    [self.view addSubview:view1];

    [view1 addSubview:tf2];
    [view1 addSubview:invoiceBtn];

它在模拟器中提供输出,并且按钮是可点击的 enter image description here

答案 1 :(得分:0)

 UITextField *tf2 = [[UITextField alloc] initWithFrame:CGRectMake(10, 10, 200, 30)];
tf2.textColor = [UIColor colorWithRed:0/256.0 green:84/256.0 blue:129/256.0 alpha:1.0];
//tf.font = [UIFont fontWithName:@"Helvetica-Bold" size:25];
tf2.backgroundColor=[UIColor whiteColor];
tf2.borderStyle=UITextBorderStyleLine;
tf2.placeholder=@"Capture Invoice";
tf2.textAlignment=UITextAlignmentCenter;


UIButton *invoiceBtn=[[UIButton alloc]initWithFrame:CGRectMake(250, tf2.frame.origin.y+55,50,30)];

UIImage *buttonImage = [UIImage imageNamed:@"camera-app-icon_1x.png"];
[invoiceBtn setImage:buttonImage forState:UIControlStateNormal];
[invoiceBtn addTarget:self action:@selector(invoiceClicked:) forControlEvents:UIControlEventTouchUpInside];

 UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0,270, 100, 40)];

[view1 addSubview:tf2];
[view1 addSubview:invoiceBtn];
[self.view addSubview:view1];

答案 2 :(得分:0)

像这样申请,

 UIButton * invoiceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
 UIImage *buttonImage = [UIImage imageNamed:@"camera-app-icon_1x.png"];
 [invoiceBtn setImage:buttonImage forState:UIControlStateNormal];
 [invoiceBtn addTarget:self 
       action:@selector(invoiceClicked:) forControlEvents:UIControlEventTouchUpInside];
 invoiceBtn.frame = CGRectMake(250, tf2.frame.origin.y+55,50,30);
 [view1 addSubview: invoiceBtn];

然后创建像

这样的动作
- (void)invoiceClicked:(UIButton*)button
{
     NSLog(@"Button  clicked.");
}

希望这适合你:)