uitableview - 是否有可能获得一个 .xib 窗口到选项卡式 Storyboard

标签 uitableview xcode4.2 storyboard uipickerview xib

我用 Xcode 编写代码的时间不长,所以我有点垃圾,基本上我已经创建了一个 .xib 并希望它出现在故事​​板中,但我真的不知道从哪里开始,因为我有一个包含 UITableViewUIPickerView 的 xib 窗口,所有代码在 xib 中都很好,但是我需要在 Storyboard .h/.m 等中添加代码吗?是否有可能,如果不是,我该如何为我的应用程序做某种下拉菜单。这是 xib 中的代码

#import <UIKit/UIKit.h>

    @interface myview : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, UIPickerViewDataSource>    


    @property (strong, nonatomic) IBOutlet UITableView* tableView;
    @property (strong, nonatomic) IBOutlet UIPickerView* pickerView;
    @property (strong, nonatomic) NSMutableArray* tableData;
    @property (strong, nonatomic) NSMutableArray* pickerData;


    @end

这是 .m,但我会将其分解

    #import "myview.h"

    @implementation myview


    @synthesize tableView, pickerView, tableData, pickerData;





    - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
        self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
        if (self) {
            // Custom initialization
        }
        return self;
    }

this is the -(void)viewDidLoad];etc..


    - (void)viewDidUnload
    {
        [super viewDidUnload];

        tableView.delegate = self;
        tableView.dataSource = self;
        pickerView.delegate = self;
        pickerView.dataSource = self;

        tableData = [[NSMutableArray alloc] init]; // table starts empty
        pickerData = [[NSMutableArray alloc] initWithObjects:@"1", @"2", @"3", @"4", @"5", nil]; // picker starts with values 1, 2, 3, 4, 5

        [tableView reloadData];
        [pickerView reloadAllComponents];    // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }

-(bool) 


        - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
        {
            // Return YES for supported orientations
            return (interfaceOrientation == UIInterfaceOrientationPortrait);
        }


        - (NSInteger)numberOfSectionsInTableView:(UITableView*)tableView {
            //The number of sections in UITableView
            return 1;

        }


        - (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
            // The number of rows in the UITableView
            return [tableData count];

        }


         - (UITableViewCell*)tableView:(UITableView*)tableView1 cellForRowAtIndexPath:(NSIndexPath *)indexPath {
                static NSString *cellIdentifier = @"cell";

           UITableViewCell *cell = [tableView1 dequeueReusableCellWithIdentifier:cellIdentifier];

                if (cell == nil) {
                    cell = [ [UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellIdentifier];

          }
    cell.textLabel.text = [tableData objectAtIndex:indexPath.row];

        return cell;

    }  


        - (void)tableView:(UITableView*)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
            // Whatever happens when you select a table view row.
        }

        - (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
        {
            // The number of sections in the UIPickerView
            return 1;
        }

        - (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
        {
            // The number of rows in the UIPickerView
            return [pickerData count];
        }

        - (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
        {
            // The data for each row in the UIPickerView
            return [pickerData objectAtIndex:row];
        }

        - (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
        {
            // whatever you want to happen when a row is selected.

            // here I am assuming you want to remove from the picker and add to the table on selection
            [tableData addObject:[pickerData objectAtIndex:row]];
            [pickerData removeObjectAtIndex:row];

            [tableView reloadData];
            [self.pickerView reloadAllComponents];   
     }




        @end

我希望这一切都是有意义的,我期待收到您的来信

非常感谢您的宝贵时间:)

最佳答案

没有下拉菜单自动转换。该过程是手动的,描述如下:

  1. 从 xib 剪切并粘贴到新的 Storyboard View Controller (打开 xib,选择全部并选择复制,然后转到 Storyboard 并将其粘贴到空 View Controller 中)。
  2. 将新的 View Controller 子类(.m 和 .h)添加到项目中。
  3. 将自定义方法从旧 View Controller 子类剪切并粘贴到新 View Controller 子类。请注意不要迁移加载 xib 的代码。
  4. 首先将新的自定义 View Controller 子类与 Storyboard中的 View Controller 关联。
  5. 连接所有 IBAction 和 IBOutlet,您就可以开始了!
  6. 您也可以寻找使用 segues 代替 IBActions 的方法。

关于uitableview - 是否有可能获得一个 .xib 窗口到选项卡式 Storyboard,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9411324/

相关文章:

ios - 将数据传递给自定义单元格中的 UIbutton

ios - 配置文件与应用程序标识符 com.domainname.helloworld 不匹配

ios - 如何设置约束以使 UIImage 的底部位于屏幕高度的 1/5?

iOS 无法更改 UIButton 的框架

ios - 在 UIView(不是 UITableViewCell)上滑动以显示操作

iphone - 有没有办法让应用程序在装有 iOS 7 的 iPhone 上运行并显示为 ios 6?

ios - UITableView 单元格分隔符在被选中时消失

iphone - 我如何在 UIView 手势下检测 UIButton 的 TouchUp 事件

ios - 在 Xcode 4.2.1 中,我应该使用什么来代替基于 Window 的应用程序模板?

storyboard - 如何在 Swift 中将类分配给自定义 UIView?