是否有可能获得一个 .xib 窗口到选项卡式故事板

发布于 2025-01-08 08:20:54 字数 4361 浏览 6 评论 0原文

我没有很长的时间来编写代码,Xcode,所以我有点垃圾,基本上我已经创建了一个.xib,并希望它出现在故事​​板中,但我真的不知道从哪里开始,因为我有一个 xib 窗口其中有 UITableViewUIPickerView ,并且所有代码在 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

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

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

i have not long bin doing code, Xcode so I'm a little rubbish basically i have created a .xib and would like it to be in a storyboard but i really don't have a glue where to start because i have a xib window that has UITableView and UIPickerView in it and all the code is fine in the xib but do i need to add code in the storyboard .h / .m etc and is it possible if not how can i do some kind of drop down menu for my app. Heres the code in the 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

This is the .m but i will break it up

    #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

i hope all this has made sense and i will look forward to hear from you

thanks so much for your time :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2025-01-15 08:20:54

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

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

There is no drop down menu automatic conversion. The process is manual, and is described below:

  1. Cut and paste from the xib to the a new storyboard view controller (open up the xib, select all and select copy, then go to storyboard and paste it into a empty view controller).
  2. Add a fresh view controller subclass (.m and .h) to the project.
  3. Cut and paste the custom method from the old view controller subclass to the new view controller subclass. Be careful not to migrate the code that loads the xib.
  4. First associate the new custom view controller subclass with the view controller in storyboard.
  5. Hook up all the IBActions and IBOutlets and you should be good to go!
  6. Optionally, you can look for ways to use segues instead of IBActions.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文