gpt4 book ai didi

ios/uipickerview : How would I have a picker view for US states

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:14:59 28 4
gpt4 key购买 nike

具有网络编程背景的IOS新手。

在 html 中,我习惯于使用 select 语句下拉带有状态(即 AK、AL、CA、MA、NY、NJ 等)的框。使用 UIpickerview 的等效语法是什么?我知道如何将选择器 View 放在 Storyboard中,但不知道如何让所有状态显示在选择器中,然后捕获所选状态。

提前感谢您的任何建议。

最佳答案

当您将 pickerView 放入 StoryBoard 中时,在您的 ViewController 中创建一个 IBOutlet,以及一个状态数组

@IBOutlet weak var myPicker: UIPickerView!
var states = ["state1", "state2","state3"]
var selectedState = ""

设置 pickerView 的委托(delegate)和数据源

override func viewDidLoad() {
super.viewDidLoad()
myPicker.dataSource = self
myPicker.delegate = self
}

调用委托(delegate)方法:

func numberOfComponentsInPickerView(pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return states.count
}

将数据放入选择器中:

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {
return states[row]
}

并捕捉选中状态:

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectedState = states[row]
}

针对 ObjC 的编辑

创建导出并为 pickerView 设置委托(delegate)和数据源非常相似:以下是委托(delegate)方法:

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
return ([states count]);
}

将数据放入选择器中:

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
return ([states objectAtIndex: row]);
}

并捕捉选中状态:

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

selectedState = ([states objectAtIndex:row]);
}

关于ios/uipickerview : How would I have a picker view for US states,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29196382/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com