(IBAction)button to label output

后端 未结 2 1033
生来不讨喜
生来不讨喜 2021-01-27 16:39

hi im new in iphone SDK object C programming.. this question i want to ask is that , how can i run a program with 2 buttons (increment 1 and decrement 2) to be shown the result

2条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2021-01-27 16:53

    Note: These are the basic things that you can get through googling itself. Check this link.

    Hints for your scenario:

    -Create a view with 2 buttons, 1 label

    -Set the IBOutlet label

    -Set tag for each buttons

    -Assign a same action[say -(IBAction) buttonAction: (id)sender] for both buttons,

    -Have one global integer varible(say val)

    -Code as follows

    -(IBAction)buttonAction: (id)sender
    {
     UIButton *but=(UIButton *)sender;
     if(but.tag==1)
     {
       val++;
       [label setText:[NSString stringWithFormat:"%@"],val];
     }
     else
     {
       val--;
       [label setText:[NSString stringWithFormat:"%@"],val];
     }
    }
    

提交回复
热议问题