How can I call an IBAction in the viewDidLoad?

2019-07-12 12:36发布

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

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

Could someone please help me out?

Thanks!

2条回答
聊天终结者
2楼-- · 2019-07-12 13:31

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 {

}

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)];
查看更多
Bombasti
3楼-- · 2019-07-12 13:31

Use [self sliderValue:nil]; in viewDidLoad

查看更多
登录 后发表回答