如何将选择器视图中选择的行从应用程序委托链接到视图控制器中的变量?

发布于 2024-12-22 21:12:23 字数 2014 浏览 1 评论 0原文

我对 UIPickerView 有疑问。我在应用程序委托中运行此方法:

- (id) init {
    if([super init] == nil) return nil;
    myGoal = [[NSArray alloc] initWithObjects:
              @"Select Goal",@"First Class Honors",@"Second Class, Upper",@"Second Class, Lower", @"Third Class Honors", @"Pass with Merit, Bachelor",@"Pass, Bachelor", nil];
    return self;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component {

    NSLog(@"picked row: %i, component: %i", row, component);
    NSLog(@"the value: %@", [self.myGoal objectAtIndex:row]);

在 viewcontroller.m 文件中,我链接了一个 IBAction 来在按下时进行计算。但为了进行计算,我需要能够知道在应用程序委托的方法中选择了哪一行。

在我的 IBAction 中:

- (IBAction) calculateGoal: (id) sender {

    **//--i want to link the selected pickerview row to a fix integer in his case the GPA. how sould i do this?** 
    if (row == 1) {
        double selectedGPA = 4.5;
    }
    if (row == 2) {
        double selectedGPA = 4.0;
    }
    if (row == 3) {
        double selectedGPA = 3.5;
    }
    if (row == 4) {
        double selectedGPA = 3.0;
    }
    if (row == 5) {
        double selectedGPA = 2.5;
    }
    if (row == 6) {
        double selectedGPA = 2.0;
    }

    //--change text to numbers
    double gpa = [txtGPA.text doubleValue];
    double totalau = [txtTotalAU.text doubleValue];
    double remainingau = [txtRemainingAU.text doubleValue];
    double x = gpa * totalau;
    double gpagoal = (selectedGPA * (totalau + remainingau) - x)/remainingau; //should replace 3.5 with a variable(selectedGPA) related with goal. First Class = 4.5 Second Class Upper = 4.0, Second Class Lower = 3.5, Third Class = 3.0, Pass with Merit = 2.5, Pass =2.0

问题: 1.在我的视图controller.m的IBAction中,我希望能够拉出当前选择的选择器(app delegate.m文件中的方法)的哪一行,以便我可以将相应的值分配给变量“selectedGPA”。

2.应用程序委托的 IBAction 方法中的 if 语句是否正确过滤掉所选行并将整数分配给变量“selectedGPA”?

i have a questions regarding the UIPickerView. i have this method running in the the app delegate:

- (id) init {
    if([super init] == nil) return nil;
    myGoal = [[NSArray alloc] initWithObjects:
              @"Select Goal",@"First Class Honors",@"Second Class, Upper",@"Second Class, Lower", @"Third Class Honors", @"Pass with Merit, Bachelor",@"Pass, Bachelor", nil];
    return self;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
       inComponent:(NSInteger)component {

    NSLog(@"picked row: %i, component: %i", row, component);
    NSLog(@"the value: %@", [self.myGoal objectAtIndex:row]);

and in the viewcontroller.m file, i have a IBAction linked to do a calculation when pressed. but in order to do the calculation i need to be able to know which row is selected in the app delegate's method.

in my IBAction:

- (IBAction) calculateGoal: (id) sender {

    **//--i want to link the selected pickerview row to a fix integer in his case the GPA. how sould i do this?** 
    if (row == 1) {
        double selectedGPA = 4.5;
    }
    if (row == 2) {
        double selectedGPA = 4.0;
    }
    if (row == 3) {
        double selectedGPA = 3.5;
    }
    if (row == 4) {
        double selectedGPA = 3.0;
    }
    if (row == 5) {
        double selectedGPA = 2.5;
    }
    if (row == 6) {
        double selectedGPA = 2.0;
    }

    //--change text to numbers
    double gpa = [txtGPA.text doubleValue];
    double totalau = [txtTotalAU.text doubleValue];
    double remainingau = [txtRemainingAU.text doubleValue];
    double x = gpa * totalau;
    double gpagoal = (selectedGPA * (totalau + remainingau) - x)/remainingau; //should replace 3.5 with a variable(selectedGPA) related with goal. First Class = 4.5 Second Class Upper = 4.0, Second Class Lower = 3.5, Third Class = 3.0, Pass with Merit = 2.5, Pass =2.0

Question:
1.in my view controller.m's IBAction, i want to be able to pull out which row the picker(a method from app delegate.m file) is currently selected so that i can assign the corresponding value to a variable "selectedGPA".

2.is my if statement in the app delegate's IBAction method correct in filtering out the row selected and assigning the integer to the variable "selectedGPA"?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

花桑 2024-12-29 21:12:23

使用委托。将 AppDelegate 设置为 UIPickerView 的委托。

self.picker.delegate = [[UIApplication sharedApplication] delegate];

Use delegate. Set the AppDelegate to be your UIPickerView's delegate.

self.picker.delegate = [[UIApplication sharedApplication] delegate];
岁月如刀 2024-12-29 21:12:23

我设法解决了我的问题。我不确定这是否是正确的方法,或者我很幸运它有效。

int row;
double selectedGPA;

row = [myPicker selectedRowInComponent:0];

//--i want to link the selected pickerview row to a fix integer in his case the GPA. how sould i do this.
if(row == 0){
    selectedGPA = 0;
}
else if (row == 1) {
    selectedGPA = 4.5;
}
else if (row == 2) {
    selectedGPA = 4.0;
}
else if (row == 3) {
    selectedGPA = 3.5;
}
else if (row == 4) {
    selectedGPA = 3.0;
}
else if (row == 5) {
    selectedGPA = 2.5;
}
else
    selectedGPA = 2.0;

i managed to solve my problem. i am not sure if this is the right way or i am just lucky it worked..

int row;
double selectedGPA;

row = [myPicker selectedRowInComponent:0];

//--i want to link the selected pickerview row to a fix integer in his case the GPA. how sould i do this.
if(row == 0){
    selectedGPA = 0;
}
else if (row == 1) {
    selectedGPA = 4.5;
}
else if (row == 2) {
    selectedGPA = 4.0;
}
else if (row == 3) {
    selectedGPA = 3.5;
}
else if (row == 4) {
    selectedGPA = 3.0;
}
else if (row == 5) {
    selectedGPA = 2.5;
}
else
    selectedGPA = 2.0;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文