如何将选择器视图中选择的行从应用程序委托链接到视图控制器中的变量?
我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用委托。将 AppDelegate 设置为 UIPickerView 的委托。
Use delegate. Set the AppDelegate to be your UIPickerView's delegate.
我设法解决了我的问题。我不确定这是否是正确的方法,或者我很幸运它有效。
i managed to solve my problem. i am not sure if this is the right way or i am just lucky it worked..