(IBAction)按钮标记输出

时间:2011-01-21 05:45:06

标签: iphone objective-c

嗨我在iphone SDK对象C编程中的新功能..我想问的问题是,如何运行带有2个按钮(增量1和减量2)的程序,以便在标签中显示结果。每次单击increment1或decrement1时结果都会改变。 对不起,对象c编程非常新,希望你能帮助我:) 谢谢

-mark

2 个答案:

答案 0 :(得分:1)

一旦熟悉Objective C和Xcode,就很容易实现。但我的建议是熟悉这些,以便其他开发人员也可以帮助您解释。

我希望你已经创建了一个试用项目来启动。它将有.h .m和.xib文件。

  1. 在你的xib文件中取两个UIButtons和一个UILabel。

  2. 将按钮和标签的插座连接到您的nib文件。

  3. 在.h和.m文件中添加以下代码。

  4. .h文件的代码

    @interface RootViewController :     UIViewController<> {
    IBOutlet UIButton *incrBtn;
    IBOutlet UIButton *decrBtn;
    IBOutlet UILabel *label;
    NSInteger counter;
    
    }
    
    -(IBAction)incr;
    -(IBAction)decr; 
    

    .m文件的代码

    - (void)viewDidLoad {
    [super viewDidLoad];
    counter=0;
    label.text=[NSString stringWithFormat:@"%d",counter];
    }
    
    -(IBAction)incr{
    counter++;
    label.text=[NSString stringWithFormat:@"%d",counter];
    
    
    }
    
    -(IBAction)decr{
    counter--;
    label.text=[NSString stringWithFormat:@"%d",counter];
    
    
    }
    

    就是这样!

答案 1 :(得分:0)

注意:这些是您通过Google搜索自己可以获得的基本内容。请检查此link

针对您的方案的提示:

使用2个按钮创建视图,1个标签

- 设置IBOutlet标签

- 为每个按钮设置标签

- 为两个按钮分配相同的动作[比如 - (IBAction)buttonAction:(id)sender],

- 有一个全局整数变量(比如val)

代码如下

-(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];
 }
}