如何在代码中接收滑块的当前值?
我正在(尝试)使用 cocoa 框架在 Xcode 中开发我的第一个应用程序。
我有一个滑块,最小值为 10,最大值为 50。这是为了选择最大搜索结果。
我在用户界面上链接了一个标签来显示滑块的值,当滑块移动时,它会更新用户界面上的标签。
但是,我尝试连接大约 4 个字符串来创建我的最终 URL,其中之一是所述标签的值。
我正在尝试读取界面上标签的值以用于创建完成的 URL
NSString *startofURL = @"http://starturl.com/?q=";
NSString *searchTerm = whatToSearch;
NSString *middleofURL = "&max-results=";
NSString *resultsStr = labelMaxResults.stringValue; //Problem here ??
我有 2 个问题;首先,我如何通过代码检索滑块的值,而不是尝试从链接的标签中获取它,因为我认为这是我的问题。
其次,我已经阅读了有关连接和附加字符串的内容,但是我有点困惑,为了将 4 个字符串连接到一个长 URL 中,最好使用哪种方法。
I am (attempting) developing my first application in Xcode using cocoa framework.
I have a slider which min value is 10 and max is 50. This is to select max search results.
I have linked a label on my user interface to display the value of the slider and when it is moved, it updates the label on the user interface.
However, I am trying to join around 4 strings to create my final URL one of them is the value of said label.
I am trying to read the value of the label on the interface for use in creating the finished URL
NSString *startofURL = @"http://starturl.com/?q=";
NSString *searchTerm = whatToSearch;
NSString *middleofURL = "&max-results=";
NSString *resultsStr = labelMaxResults.stringValue; //Problem here ??
I have 2 questions; firstly, How do I go about retrieving the value of my slider via code instead of trying to get it from the linked label, as I think this is my problem.
secondly, I have read up on joining and appending strings, however I am a little confused on which is the best method to use in order to join up the 4 strings into one long URL.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这不会将你的滑块放在屏幕上,但假设你是在 IB 中制作的,忽略第一行,你可以设置你的最小值最大值并获取值。
你可以采取类似的行动
然后将其绑定到滑块,如果将滑块设置为连续,则每次它移动时,当您放开滑块时,您都会获得更新
this doesn't put your slider on screen, but assume you made it in IB, ignore the first line, you can set your min max and get the value.
you can make an action like
then bind that to the slider, if you set the slider to continuous you will get updates every time that it moves other wise just when you let go of the slider
对于问题的第一部分,取决于您使用的操作系统以及使用哪个进程。
如果您使用的是 OS X,获取滑块值的最佳方法是在 IB 中为滑块创建绑定。为此,请单击 IB 中的滑块并转到绑定部分。在“值”部分中,单击“值”并选择您希望使用该值作为“绑定到”类的类(在本例中我将使用 MyClass)。然后对于模型关键路径,将其分配给您选择的某个值。为此,我将其称为 sliderValue。
然后在 MyClass.h 中,您必须设置以下内容:
在 MyClass.m 中,您需要综合该值。
此时,您应该能够通过调用
[self sliderValue]
在代码中的任意位置获取滑块的值。但是,如果您使用的是 iOS,那么您所要做的就是从滑块中调用 value 属性。因此,如果您有
UISlider *mySlider
,您所要做的就是调用mySlider.value
来获取滑块的当前值。对于你的第二个问题,你可以从两个方面来考虑。如果要附加字符串,只需遵循以下格式:
依此类推,直到将所有字符串添加到 URL 中。
在您的情况下,我个人会将整个 URL 字符串设置为 stringWithFormat:
通过将 WhatToSearch 等设置为您想要的值,以这种方式将您想要的值插入到 URL 中。这样,您就不必担心将所有内容附加在一起
For the first part of your question, it depends which OS you are using as to which process to use.
If you are using OS X, the best way to get a value for your slider is to create a binding in IB for your slider. To do this, click on the slider in IB and go to the bindings section. In the Value section, click on Value and select the class you wish to use the value in as your Bind To class (I'm going to use MyClass for this example). Then for the model key path, assign it to some value of your choice. For the purpose of this, I'll just call it sliderValue.
Then in MyClass.h, you must set up the following:
In MyClass.m, you'll need to synthesize the value.
At this point, you should be able to get the value of your slider at any point in your code by calling
[self sliderValue]
.If you are, however, using iOS, then all you have to do is call the value property from your slider. So if you have the a
UISlider *mySlider
, all you have to do is callmySlider.value
to get the current value of your slider.For your second question, you can go about this two ways. If you want to append the strings, simply follow the format:
and so on until you have all your strings into your URL.
In your case, I would personally set up the entire URL string as a stringWithFormat:
Insert the values you want into the URL that way by setting up whatToSearch, etc., to the values you want. This way, you don't have to worry about appending everything together
您的代码应如下所示:
编辑:更正了我的答案。对于 UISlider 它应该是 value 属性。对于 NSSlider 你应该使用
- floatValue
方法Your code should look like this:
EDIT: Corrected my answer. For UISlider it should be value property. For NSSlider you should use
- floatValue
method