c# - MonoTouch (Xamarin) 中的简单 UIPickerView?

标签 c# ios objective-c xamarin.ios uipickerview

谁能描述我如何使用 XCode 在单点触摸中创建 UIPickerView 并用示例数据填充它?

我确实看过这里的例子:https://github.com/xamarin/monotouch-samples/blob/master/MonoCatalog-MonoDevelop/PickerViewController.xib.cs但这并不是很有帮助,因为我在 XCode 中创建了我的 UIPickerView。这是我到目前为止所拥有的:

public partial class StatusPickerPopoverView : UIViewController
{
    public StatusPickerPopoverView (IntPtr handle) : base (handle)
    {
        pickerStatus = new UIPickerView();
        pickerStatus.Model = new StatusPickerViewModel();
    }

    public StatusPickerPopoverView (): base ()
    {
    }

    public class StatusPickerViewModel : UIPickerViewModel
    {
        public override int GetComponentCount (UIPickerView picker)
        {
            return 1;
        }

        public override int GetRowsInComponent (UIPickerView picker, int component)
        {
            return 5;
        }

        public override string GetTitle (UIPickerView picker, int row, int component)
        {

            return "Component " + row.ToString();
        }
    }
}

最佳答案

所以我基本上找到了答案,它比你想象的要容易得多!

模型必须在 ViewDidLoad 中设置,否则会崩溃...这就是它没有被正确填充的原因。记住:在“ViewDidLoad”中设置。

public partial class StatusPickerPopoverView : UIViewController
{
    public StatusPickerPopoverView (IntPtr handle) : base (handle)
    {
    }

    public override ViewDidLoad()
    {
        base.ViewDidLoad();

        pickerStatus = new UIPickerView();
        pickerStatus.Model = new StatusPickerViewModel();
    }

    public class StatusPickerViewModel : UIPickerViewModel
    {
        public override int GetComponentCount (UIPickerView picker)
        {
            return 1;
        }

        public override int GetRowsInComponent (UIPickerView picker, int component)
        {
            return 5;
        }

        public override string GetTitle (UIPickerView picker, int row, int component)
        {

            return "Component " + row.ToString();
        }
    }
}

关于c# - MonoTouch (Xamarin) 中的简单 UIPickerView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15577389/

相关文章:

ios - MPMoviePlayer 未从 super View 中删除

ios - 触摸按钮时如何使用 UIScrollview 滚动到页面

iOs 注册来自相机胶卷的通知

ios - 将 Storyboard View 中的所有内容移动到 ScrollView 中

c# - 在 C# 中使用 LINQ 进行字典操作

c# - DataTemplate 内的 DataTemplate - ListBox 内的 ListBox

c# - 从其他 Xaml 文件绑定(bind)到 Usercontrol 中的元素

c# - 如何将数据从 C# 传递到 C++(仅指针,无编码)

ios - UITableViewCell 约束未更新

IOS 将值从 App Delegate 传递到初始 viewcontroller 并在 viewWillAppear 中使用这些值