1.創建項目
創建初始場景,和日期選擇場景(可以將其背景色設置為Scroll View Texted Background Color),選擇一個日期選擇器至該視圖
2.創建切換
按住CONTROL從初始視圖拉向日期設置視圖,(注意與前一章的區別,這里因為是兩個控制器相連,所以要手工觸發切換因此將該切換命名以便代碼實現)
3.實現邏輯
1.在實現中除了讓兩個控制器知道彼此的方法屬性外,還要提供一個屬性(讓日期選擇器能夠訪問初始控制器,他將通過該屬性訪問初始控制器,因為在IPAD中要禁止用戶同時顯示多個彈出框,若只是用模態切換則可以上一章一樣用presentingViewController來獲取初始場景視圖控制器,但其不適用彈出框)
這里我暫且只關注IPHONE
2.手工切換的方法
由於手工切換,所以要在相應轉換按鈕按下的方法中編寫代碼
首先你要檢查當前是否已經顯示了日期選擇器視圖,通過設置一個布爾屬性來進行判斷,在初始控制器頭文件中添加
@property(nonatomic) Boolean dateChooserVisible
布爾不是對象,所以聲明屬性時不用使用關鍵字Strong也不需要使用后將其設置為NIL,
-(IBACTION)show:id(sender){
if(self.dataChooserVisible != YES)
{
[self performSegueWithIdentifier:@"toDataChooser"sender:sender];//啟動標識符為TODATACHOOSER的轉換,sender為啟動切換的對象
self.dataChooserVisble = Yes;
}
}
打開了日期選擇界面后相應的只是bool變為了YES必須在該界面關閉時將其改回NO,
-(void)viewWillDisappear:(bool)animated{ //該方法在視圖關閉時發生
((viewController *)self.delegate).datechooservisible = NO; //通過屬性訪問初始視圖中的變量BOOL將其改回NO
}
關閉模態場景
-(IBACTION)dismiss自己定義的關閉按鈕
{
[self dismissViewControllerAnimated:YES completetion:nil];
}
實現自定義選擇視圖
在相應的頭文件中聲明協議並設置屬性
@interface AnimalChooserViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>
@property (strong, nonatomic) NSArray *animalNames;
@property (strong, nonatomic) NSArray *animalSounds;
@property (strong, nonatomic) NSArray *animalImages;
.m文件中加載視圖
- (void)viewDidLoad
{
self.animalNames=[[NSArray alloc]initWithObjects:
@"Mouse",@"Goose",@"Cat",@"Dog",@"Snake",@"Bear",@"Pig",nil];
self.animalSounds=[[NSArray alloc]initWithObjects:
@"Oink",@"Rawr",@"Ssss",@"Roof",@"Meow",@"Honk",@"Squeak",nil];
self.animalImages=[[NSArray alloc]initWithObjects:
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"mouse.png"]],
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"goose.png"]],
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"cat.png"]],
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"dog.png"]],
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"snake.png"]],
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bear.png"]],
[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pig.png"]],
nil
];
[super viewDidLoad];
}
記住要在DidUnLoad中清除
- (void)viewDidUnload
{
[self setDelegate:nil];
[self setAnimalNames:nil];
[self setAnimalImages:nil];
[self setAnimalSounds:nil];
[super viewDidUnload];
}
現在.m文件中聲明
#define kComponentCount 2
#define kAnimalComponent 0
#define kSoundComponent 1
實現選擇器視圖數據源協議
返回組件數(.m文件)
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
return kComponentCount;
}
返回每個組件包含的元素數
- (NSInteger)pickerView:(UIPickerView *)pickerView
numberOfRowsInComponent:(NSInteger)component {
if (component==kAnimalComponent) {
return [self.animalNames count];
} else {
return [self.animalSounds count];
}
}
實現選擇器視圖委托協議
給每個選擇器提供自定義視圖
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row
forComponent:(NSInteger)component reusingView:(UIView *)view {
if (component==kAnimalComponent) { //檢查是否動物組件,若是根據參數row返回數組animal images中相應的UImageView
return [self.animalImages objectAtIndex:row]; //若不是則使用animalSOunds數組中相應元素創建一個UILABle並返回
} else {
UILabel *soundLabel;
soundLabel=[[UILabel alloc] initWithFrame:CGRectMake(0,0,100,32)]; //shi使用initWUithFrame發放初始化UIlable,創建標簽需定義其框架對應的矩形,CGrectMake定義起點0,0寬100高32的矩形
// [soundLabel autorelease];
soundLabel.backgroundColor=[UIColor clearColor]; //設置背景色透明
soundLabel.text=[self.animalSounds objectAtIndex:row];
return soundLabel;
}
}
選擇器組件的寬度,行高,不設置顯得擁擠
- (CGFloat)pickerView:(UIPickerView *)pickerView
rowHeightForComponent:(NSInteger)component {
return 55.0;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
if (component==kAnimalComponent) {
return 75.0;
} else {
return 150.0;
}
}
在用戶做出選擇時響應
我們用事件Value Changed來捕獲用戶修改選擇器的操作,但自定義選擇器的工作原理不是這樣。
要獲取用戶自定義選擇器中所做的選擇必須實現委托方法pickerView:didSelectRow:inComponent
先在.H頭文件中添加方法模型(源視圖.h中)
- (void)displayAnimal:(NSString *)chosenAnimal withSound:(NSString *)chosenSound fromComponent:(NSString *)chosenComponent;
創建一個將用戶的選擇情況顯示出來的方法(源視圖.M中)
- (void)displayAnimal:(NSString *)chosenAnimal withSound:(NSString *)chosenSound fromComponent:(NSString *)chosenComponent {
NSString *animalSoundString;
animalSoundString=[[NSString alloc]
initWithFormat:@"You changed %@ (%@ and the sound %@)",
chosenComponent,chosenAnimal,chosenSound];
self.outputLabel.text=animalSoundString;
}
目的控制器。m文件中 實現方法
- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row
inComponent:(NSInteger)component {
ViewController *initialView;
initialView=(ViewController *)self.delegate;
if (component==kAnimalComponent) {
int chosenSound=[pickerView selectedRowInComponent:kSoundComponent]; //添加這行的原因是,- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row 方法只能指定最后用戶選到的選項而不能收集到所有的狀態
inComponent:(NSInteger)component {
[initialView displayAnimal:[self.animalNames objectAtIndex:row]
withSound:[self.animalSounds objectAtIndex:chosenSound]
fromComponent:@"the Animal"];
} else {
int chosenAnimal=[pickerView selectedRowInComponent:kAnimalComponent];
[initialView displayAnimal:[self.animalNames objectAtIndex:chosenAnimal]
withSound:[self.animalSounds objectAtIndex:row]
fromComponent:@"the Sound"];
}
}
指定默認情況(目的控制器.m)
-(void)viewDidAppear:(BOOL)animated {
ViewController *initialView;
initialView=(ViewController *)self.delegate;
[initialView displayAnimal:[self.animalNames objectAtIndex:0]
withSound:[self.animalSounds objectAtIndex:0]
fromComponent:@"nothing yet..."];
}