객관적인 C 코딩에 익숙해지면서, 하나의 파일에 내 AppViewController
h 및 m 파일로 완전히 프로그래밍 방식으로 (스토리 보드 또는 xib를 사용하지 않고) 기본 앱을 작성하기 시작했습니다. 모든 것이 멋지게 작동했습니다.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 시스템 중 하나와 함께 할 것입니다 이러한 라인을 따라 오류의 유형 그러나에 내가 발견 한 답변 없음 관련 또는 작업을 제공합니다 내가 위에있는 것을 세운다. 나는 그것이 정말로 단순한 무엇인가 확실하다. 그러나 실패한 수일의 검색 이후 그것은 공식적으로 나를 미치게한다.
이것은 참으로 문제점을 고쳤다! Picker * picker를 UIPickerViewController.h의 클래스 변수로 추가하고이를 수용하기 위해 UIPickerViewController.m 파일에서 호출을 수정했습니다. 우리는 다시 게임에 참가했습니다! 실제 프로젝트에서 왼쪽, 중간 및 오른쪽 열 및 빈 팝업보기에 대한 uiview 클래스를 가져 오는 projectVC가 있습니다. 모두 하위 뷰로 추가되었습니다. 왼쪽 열에는 이전에 추가 한 팝업보기 (태그 8)에 쓰는 팝업 방법을 시작하는 버튼이 있습니다. 더 좋은 방법이있는 경우 기꺼이 탑승합니다. 이것은 우리가 올바르게 배우는 방법입니다. –
Right :) 엄지 손가락 규칙에 대한 업데이트 된 답변을 확인하십시오. 그리고 더 많이 알 필요가 있으면 알려주세요! – Toastor
조언을 주셔서 대단히 감사합니다. 그것은 모두 타고 있습니다. 따라서이 경우 프로젝트를 관리 가능한 덩어리로 분해하려고 시도하지 않고 viewcontroller 클래스에 모든 코드를 그대로 두는 것이 낫습니다. 데이터베이스를 검사하고 삽입, 업데이트, 선택 및 일반 SQL 관련 호출을 삭제하는 SQL 데이터 및 메서드를 저장하는 NSObject 클래스가 있습니다. 내 전체 프로젝트에서 나는 이것이 당신이 주어진 조언을 따르는 유일한 클래스라고 생각합니다 : P –