2012-02-22 67 views
0

我已經在Xcode 4.2中創建了一個xib文件,我正在尋找它到選項卡式應用程序,但我一直在嘗試,並且我對Xcode有點新,編碼這樣就是爲什麼我需要你的幫助我有我的代碼在下面,但是有一些小錯誤,但是我希望你能幫助我的是可能得到一個選項卡式應用程序......並告訴我該怎麼做。繼承人一個視頻給你看看,如果我沒有解釋它正確的情況下(這是我的視頻我只是爲這個問題http://www.youtube.com/watch?v=e7R5_kGClM0希望視頻幫助,但更重要的是你可以幫助我在xib文件中使用UIpickerview

有.H但在這個

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

這是 - (無效)viewDidLoad中];等等。

- (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 

這裏是錯誤的代碼中有兩個

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier]; 
          ^^^^^^^^^^^^^^^^^ 
     **Local declaration of 'tableView hides instance variable** 

二錯誤

[pickerView reloadAllComponents]; 
      ^^^^^^^^^^^^^^ 
Local declaration of 'pickerView hides instance variable 

非常感謝我希望在這裏由你很快!!!

回答

1

使用此項,如果局部變量和實例變量具有相同的名稱,則出現「tableView隱藏實例變量」。

- (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; 

} 

和pickerView使用此

[self.pickerView reloadAllComponents];

+0

謝謝男人:)! – 2012-02-23 09:46:15