如何在代码中接收滑块的当前值?

发布于 2024-11-04 20:52:17 字数 598 浏览 0 评论 0原文

我正在(尝试)使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无远思近则忧 2024-11-11 20:52:18
    NSSlider * slider = [[NSSlider alloc] init];
    [slider setMinValue:50];
    [slider setMaxValue:150];
    int sliderValue = [slider intValue];

这不会将你的滑块放在屏幕上,但假设你是在 IB 中制作的,忽略第一行,你可以设置你的最小值最大值并获取值。
你可以采取类似的行动

-(IBAction)sliderMoved:(id)发件人

然后将其绑定到滑块,如果将滑块设置为连续,则每次它移动时,当您放开滑块时,您都会获得更新

-(IBAction)sliderMoved:(id)sender
{
    sliderValue = [slider intValue];
    [self doSomethingElseNow];
}
    NSSlider * slider = [[NSSlider alloc] init];
    [slider setMinValue:50];
    [slider setMaxValue:150];
    int sliderValue = [slider intValue];

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

-(IBAction)sliderMoved:(id)sender

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

-(IBAction)sliderMoved:(id)sender
{
    sliderValue = [slider intValue];
    [self doSomethingElseNow];
}
贵在坚持 2024-11-11 20:52:18

对于问题的第一部分,取决于您使用的操作系统以及使用哪个进程。

如果您使用的是 OS X,获取滑块值的最佳方法是在 IB 中为滑块创建绑定。为此,请单击 IB 中的滑块并转到绑定部分。在“值”部分中,单击“值”并选择您希望使用该值作为“绑定到”类的类(在本例中我将使用 MyClass)。然后对于模型关键路径,将其分配给您选择的某个值。为此,我将其称为 sliderValue。

然后在 MyClass.h 中,您必须设置以下内容:

@interface MyClass : <your class type> {
    int sliderValue;
}
@property (readwrite, assign) int sliderValue;

在 MyClass.m 中,您需要综合该值。

@synthesize sliderValue;

此时,您应该能够通过调用 [self sliderValue] 在代码中的任意位置获取滑块的值。

但是,如果您使用的是 iOS,那么您所要做的就是从滑块中调用 value 属性。因此,如果您有 UISlider *mySlider,您所要做的就是调用 mySlider.value 来获取滑块的当前值。

对于你的第二个问题,你可以从两个方面来考虑。如果要附加字符串,只需遵循以下格式:

NSString *appendedString = @"";
appendedString = [appendedString stringByAppendingString:string1];

依此类推,直到将所有字符串添加到 URL 中。

在您的情况下,我个人会将整个 URL 字符串设置为 stringWithFormat:

NSString *urlString = [NSString stringWithFormat:@"http://www.starturl.com/%@%@%@", whatToSearch, middleOfURL, resultsStr];

通过将 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:

@interface MyClass : <your class type> {
    int sliderValue;
}
@property (readwrite, assign) int sliderValue;

In MyClass.m, you'll need to synthesize the value.

@synthesize sliderValue;

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 call mySlider.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:

NSString *appendedString = @"";
appendedString = [appendedString stringByAppendingString:string1];

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:

NSString *urlString = [NSString stringWithFormat:@"http://www.starturl.com/%@%@%@", whatToSearch, middleOfURL, resultsStr];

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

新一帅帅 2024-11-11 20:52:18

您的代码应如下所示:

NSString *urlString = [NSString stringWithFormat:@"http://starturl.com/?q=%@&max-results=%f", whatToSearch, [slider floatValue]];

编辑:更正了我的答案。对于 UISlider 它应该是 value 属性。对于 NSSlider 你应该使用 - floatValue 方法

Your code should look like this:

NSString *urlString = [NSString stringWithFormat:@"http://starturl.com/?q=%@&max-results=%f", whatToSearch, [slider floatValue]];

EDIT: Corrected my answer. For UISlider it should be value property. For NSSlider you should use - floatValue method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文