作爲新的客觀-C編碼我開始寫一個基本的應用程序,完全以編程方式(不使用故事板或XIB)在一個文件中,我AppViewController
H和M文件。 一切工作可愛。UIPicker查看子類的工作不IOS
於是我就想通過繼承的部分分手的代碼質量,並且一切順利除了UIPickerView。其實簡單地註釋掉[background addSubview:colorPicker];
似乎完全解決這個問題。我從來沒有在網上找到答案,所以我開始製作一份新文件來複制上述問題。 所以這裏有雲:
UIPickerViewController.h
#import <UIKit/UIKit.h>
#import "Picker.h"
@interface UIPickerViewController : UIViewController
@end
僅需輸入我的新類。
UIPickerViewController.m
#import "UIPickerViewController.h"
@interface UIPickerViewController()
@end
@implementation UIPickerViewController
- (void)viewDidLoad
{
[super viewDidLoad];
UIView *superview = self.view;
int height = superview.bounds.size.height;
int width = superview.bounds.size.width;
CGRect popupRect = CGRectMake(0, 0, width, height);
UIView *popup = [[UIView alloc]initWithFrame:popupRect];
popup.tag = 8;
[superview addSubview:popup];
Picker *picker = [[Picker alloc]initWithFrame:popupRect];
[picker viewAddTypeScreenToView:superview];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
@end
建立與標籤(這樣我可以與我的新階級隨後參考) 然後行動從我的新類的方法來填充我的新的新觀點視圖。
Picker.h
#import <UIKit/UIKit.h>
@interface Picker : UIView
<UIPickerViewDataSource,UIPickerViewDelegate>
{
UIPickerView *colorPicker;
NSMutableArray *colorsArray;
}
@property (nonatomic, retain) UIPickerView *colorPicker;
@property (nonatomic, retain) NSMutableArray *colorsArray;
@property (strong,nonatomic) UILabel *myValue;
-(void)viewAddTypeScreenToView:(UIView*)superview;
@end
設置我的變量和使用方法。
Picker.m
#import "Picker.h"
@implementation Picker
@synthesize colorsArray;
@synthesize colorPicker;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
}
return self;
}
-(void)viewAddTypeScreenToView:(UIView*)superview
{
UIView *baseView =[superview viewWithTag:8];
int height = baseView.bounds.size.height;
int width = baseView.bounds.size.width;
CGRect fullScreen = CGRectMake(0, 0, width, height);
UIView *background = [[UIView alloc]initWithFrame:fullScreen];
background.backgroundColor = [UIColor blackColor];
colorsArray = [[NSMutableArray alloc] initWithObjects:@"Red",@"Blue",@"Yellow",@"Green",nil];
CGRect myPickerRect = CGRectMake(10, 70, (width/2)-40, 200);
colorPicker = [[UIPickerView alloc]initWithFrame:myPickerRect];
colorPicker.dataSource = self;
colorPicker.delegate = self;
colorPicker.showsSelectionIndicator = YES;
[colorPicker selectRow:2 inComponent:0 animated:YES];
CGRect labelFrame = CGRectMake(10, 10, 180, 50);
_myValue = [[UILabel alloc]initWithFrame:labelFrame];
_myValue.textColor = [UIColor redColor];
_myValue.text = @"select colour";
[background addSubview:_myValue];
[background addSubview:colorPicker];
[baseView addSubview:background];
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
return 1;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
return colorsArray.count;;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
return colorsArray[row];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
_myValue.text = [NSString stringWithString:colorsArray[row]];
}
@end
最後由選擇器類文件的方法稱爲啓動。 這給了我沿着這些線路
-[UITableViewCellContentView pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance 0x8f2b000
2014-03-19 10:29:48.407 Briefcase[1800:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellContentView pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance 0x8f2b000'
這是我讀過的是無論是數據源,或ARC系統做了一個錯誤,但是,沒有,我已經找到了應對的涉及或工作用的類型設置我以上。我確信這是一件非常簡單的事情,但經過幾天搜索失敗後,它正式讓我發瘋。
這確實解決了問題! 我在UIPickerViewController.h中添加了Picker * picker作爲類變量,並修改了UIPickerViewController.m文件中的調用以適應此 - 並且我們又回到了遊戲中! 在我的實際項目中,我有一個projectVC,它爲Left,Mid和Right列和一個空的彈出視圖導入uiview類 - 全部添加爲子視圖。 左列有一個按鈕,用於啓動彈出方法,該方法寫入先前添加的彈出視圖(標記8)。 如果有更好的解決方法,它會很樂意在船上。這是我們如何學習的正確:) –
正確:)請參閱我更新的答案,瞭解一些經驗法則。如果您需要了解更多信息,請告訴我們! – Toastor
非常感謝您的建議。這一切都採取了船上。所以在這種情況下,我會更好地將viewcontroller類中的所有代碼保留原來的樣子,而不是試圖將項目分解成可管理的塊? 我有一個NSObject類存儲SQL數據和方法來檢查數據庫,插入,更新,選擇和刪除任何通用的SQL相關的調用。在我的整個項目中,我相信這是唯一遵循您所提建議的課程:P –