如何在 viewDidLoad 中调用 IBAction? [英] How can I call an IBAction in the viewDidLoad?

查看:21
本文介绍了如何在 viewDidLoad 中调用 IBAction?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的滑块应用.当滑块发生变化时,新值会显示在 UILabel 中.效果很好!

I have a simple slider app. When the slider is changed, the new value is displayed in a UILabel. That works great!

当该视图被加载时,虽然标签是空白的.我希望 sliderValue.text 等于默认滑块值.

When that view is loaded though the label is blank. I want the sliderValue.text to be equal to the default slider value.

这是我现在的代码(以及我之前的尝试):

Here is my code right now (along with my previous attempt):

-(IBAction)sliderValue:(UISlider *)sender {
    sliderLabel.text = [NSString stringWithFormat:@"%g", roundf(slider.value)];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    -(IBAction)sliderValue:(id)sender;    
}

有人可以帮我吗?

谢谢!

推荐答案

只要调用 [self sliderValue:nil]; 如果你不需要 sender 作为一个在您的操作参数中,您可以在为 iOS 开发时省略它们.

Just call [self sliderValue:nil]; also if you don't need sender as a in parameter to your actions you can omit them when developing for iOS.

- (IBAction)sliderValue {

}

更好的方法(在我看来)是直接在 viewDidLoad 中设置值,以防您稍后向 sliderValue 方法添加内容.这真的是两件不同的事情,一个应该处理滑块更改值的逻辑,而奇数只是设置默认值.

Better yet (in my opinion) would be to set the value in viewDidLoad directly in case you add stuff later to sliderValue method. It's really two different thing one should handle logic for the slider changing value while the odder just sets the default value.

sliderLabel.text = [NSString stringWithFormat:@"%g", roundf(slider.value)];

这篇关于如何在 viewDidLoad 中调用 IBAction?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆