xcode 如何以编程方式设置 UISlider 初始值,或者,iOS 程序如何启动?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13486850/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:08:39  来源:igfitidea点击:

How to programmatically set UISlider initial value, or, how does an iOS program start up?

iosxcodestartupuislider

提问by Steve Ravet

I have been developing in xCode for exactly 3 days now. The last time I did UI development it was in Windows 3.1 with the Petzold book. Regardless of that I have my first iOS app up and running and it basically does what I need. I have two sliders to select hue and saturation, and in response to them I dynamically draw a bunch of gradient shaded circles. I got that much running between the Hello World example and stackoverflow, including caching the gradient in CGLayer (so thanks to all the stackoverflow people). There is one little piece that I can't quite get right though:

我已经在 xCode 中开发了整整 3 天。我最后一次进行 UI 开发是在 Windows 3.1 中使用 Petzold 的书。无论如何,我已经启动并运行了我的第一个 iOS 应用程序,它基本上可以满足我的需求。我有两个滑块来选择色调和饱和度,为了响应它们,我动态地绘制了一堆渐变阴影圆圈。我在 Hello World 示例和 stackoverflow 之间运行了很多,包括在 CGLayer 中缓存渐变(感谢所有 stackoverflow 人员)。不过,有一小部分我不太正确:

I want to set the initial value of one slider to 1.0 instead of the default 0.5. I know I can do that in IB, but I prefer to code it and I think I'd like to move away from IB altogether. I don't really understand how it makes connections between things and I'd like to understand the code. I have code like this to try to set it:

我想将一个滑块的初始值设置为 1.0 而不是默认的 0.5。我知道我可以在 IB 中做到这一点,但我更喜欢编码它,我想我想完全远离 IB。我真的不明白它是如何在事物之间建立联系的,我想了解代码。我有这样的代码来尝试设置它:

- (void)viewDidLoad
{
    NSLog(@"viewDidLoad");
    [super viewDidLoad];
    [hue_slider setValue:0.5];
    [sat_slider setValue:1.0];
    self.led_view.hue_slider_value=0.5;
    self.led_view.sat_slider_value=1.0;
// Do any additional setup after loading the view, typically from a nib.
}

sat_slider still ends up in the middle instead of at the end (1.0 is the max value). From stackexchange reading I understand that I am probably calling this at the wrong time, that the slider hasn't really been loaded when viewDidLoad is called, and my initial value is overwritten by the one specified in IB. What I haven't seen though is where the call should be made. So the question:

sat_slider 仍然位于中间而不是末尾(1.0 是最大值)。从 stackexchange 阅读中我了解到我可能在错误的时间调用了它,在调用 viewDidLoad 时滑块并未真正加载,并且我的初始值被 IB 中指定的值覆盖。我还没有看到应该在哪里打电话。所以问题是:

Where in my program should I put

我应该把我的程序放在哪里

[sat_slider setValue:1.0];

so that it sets the initial value of the slider, overwriting the default in IB? Can someone explain the order of how things start up in an iOS program? And a pointer to online or printed books regarding iOS and Objective C programming would be great.

以便它设置滑块的初始值,覆盖 IB 中的默认值?有人可以解释一下 iOS 程序中的启动顺序吗?一个指向有关 iOS 和 Objective C 编程的在线或印刷书籍的指针会很棒。

Edit

编辑

When I check the value of sat_slider it is nil. So that means a connection is missing? I dragged it in the storyboard and created an IBOutlet in addition to an action.

当我检查 sat_slider 的值时,它为零。所以这意味着缺少连接?我将它拖到情节提要中,并在一个动作之外创建了一个 IBOutlet。

@interface led_testViewController : UIViewController

- (IBAction)saturation_scroll:(id)sender;
- (IBAction)hue_scroll:(id)sender;

@property (retain, nonatomic) IBOutlet UISlider *hue_slider;
@property (retain, nonatomic) IBOutlet UISlider *sat_slider;
@property (strong, nonatomic) IBOutlet led_view *led_view;

@end

回答by Daniel Gao

You may put the code in - (void)viewWillAppear:(BOOL)animated;

你可以把代码放在 - (void)viewWillAppear:(BOOL)animated;

回答by Steve Ravet

I followed the suggestions of ABC and NJones to check sat_slider and it was nil. There were properties for both sat_slider and hue_slider in the ViewController.h file, but something was still missing. I deleted the properties, re-dragged them in IB, and then I was able to set the slider position in viewDidLoad with no problems.

我按照 ABC 和 NJones 的建议检查了 sat_slider,结果为零。ViewController.h 文件中有 sat_slider 和 Hue_slider 的属性,但仍然缺少某些内容。我删除了属性,在 IB 中重新拖动它们,然后我能够在 viewDidLoad 中设置滑块位置,没有任何问题。

回答by iDev

Check if sat_slidernil. Also make sure that it is properly connected in IB. If not, remove it and add it again in nib/storyboard. That should fix the issue.

检查是否sat_slider为零。还要确保它在 IB 中正确连接。如果没有,请将其删除并再次添加到笔尖/故事板中。那应该可以解决问题。