Xcode 4.2 Xib 下拉菜单

发布于 2025-01-08 10:16:08 字数 4044 浏览 1 评论 0原文

我创建了一个 .xib 文件,但遇到了一些问题。

制作了一个视频来向您展示我的情况。我使用的软件是 Xcode 4.2,它是一个 iOS 应用程序。

这是我认为的代码副本 .h .m

#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


#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;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (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)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*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";

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

    }

    // Set the table cell text to the appropriate value in tableDate
    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];
    [pickerView reloadAllComponents];
}
@end

I have created a .xib file and I'm having a little problem.

I did a video to show you where I'm at with it. The software I am using is Xcode 4.2 and its an iOS application.

Here is a copy of the code in my view .h .m

#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


#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;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (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)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*)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cell";

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

    }

    // Set the table cell text to the appropriate value in tableDate
    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];
    [pickerView reloadAllComponents];
}
@end

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

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

发布评论

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

评论(1

深海夜未眠 2025-01-15 10:16:08

我知道这是非常旧的并且视频已被删除,所以我不确定确切的问题是什么,但它肯定与您在 viewDidUnload 中实例化所有内容有关...您应该在最有可能的是您的viewDidLoad

I know this is super old and the video is removed so I am not sure what the exact problem is but it is most certainly related to you instantiating everything in our viewDidUnload... You should be doing this in your viewDidLoad most likely.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文