我一直在研究新的提示&为一款名为Dofus的游戏制作项目。在第一个视图中,要求用户从选择器中选择他们的类。然后他们进入下一个视图并选择他们的级别和类似的东西。然而,选择器的问题在于它从选择器中获取了错误的类。
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
self.classNames = @[@"Cra", @"Ecaflip", @"Eliotrope", @"Eniripsa",
@"Enutrof", @"Feca", @"Foggernout", @"Iop", @"Masqueraider", @"Osamodas", @"Pandawa", @"Rogue", @"Sacrier", @"Sadida", @"Sram", @"Xelor"];
}
在ViewDidLoad中我声明了我的类:
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
// getting an NSString
NSString *myClass = [prefs stringForKey:@"class"];
_textLabel.text = [NSString stringWithFormat:@"So you are a %@ huh? Pretty cool! Its a good class. Fill out the rest so we are ready to go!", myClass];
}
然后在下一个视图中,我得到这样的信息(不同的.m文件):
{{1}}
任何人都知道这是怎么回事?提前谢谢!
答案 0 :(得分:0)
您正在针对行标题的Picker数据源调用设置首选项,而不是选择行时的委托调用。对于加载到视图中的每一行,都会调用select
*
from
(
select
*,
(
'111.045' * DEGREES(
ACOS(
COS(
RADIANS('45.5088')
) * COS(
RADIANS(lat)
) * COS(
RADIANS('-73.5878' - lng)
) + SIN(
RADIANS('45.5088')
) * SIN(
RADIANS(lat)
)
)
)
) AS distance
from
`places`
where lat BETWEEN 45.418746 AND 45.598854
and lng BETWEEN -73.716301 AND -73.459299
) as d
where `distance` <= 10
order by `distance` asc
,此时您会设置pickerView:titleForRow:forComponent:
首选项,但是,这与选择器中选择的行不对应,它对应于下一行要显示。
相反,回应
class
方法,并在那里设置您的偏好(您仍然需要回复另一个来设置行标题,只是不要设置首选项。)
pickerView:didSelectRow:inComponent:
答案 1 :(得分:0)
如果您希望用户存储所选行,则应使用UIPickerViewDelegate
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
;
这样的事情会起作用:
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *_myClass = self.classNames[row];
[prefs setObject:_myClass forKey:@"class"];
[prefs synchronize];
}
}
看一下这些链接,以便更好地了解UIPickerView的工作原理:
http://nscookbook.com/2013/01/ios-programming-recipe-7-using-the-uipickerview/
http://www.techotopia.com/index.php/An_iOS_7_UIPickerView_Example