根據(jù)需求文檔,要實(shí)現(xiàn)一個(gè)彈框選擇器,本來想在網(wǎng)上找一個(gè)人家寫好的拿來用用的,但是最近收到多方打擊,決定動(dòng)手寫一個(gè)。雖然很簡單,但是最起碼是自己的東西,同時(shí)呢,也希望能給其他人帶來一丁點(diǎn)的用處。
需求說明:
1.點(diǎn)擊選擇按鈕時(shí),彈出選擇框,選擇之后顯示選擇的內(nèi)容。
2.iOS要求使用拾取器來實(shí)現(xiàn)。··
簡單的效果圖如下:
IMG_1175.PNG
界面沒什么好說的,我就來說說里面的代理吧。代理在開發(fā)過程中用到的很多的,也是一個(gè)很少用的設(shè)計(jì)模式。
首先要在pickViewController.h文件中聲明一個(gè)協(xié)議;
@protocol pickViewDelegate <NSObject>
- (void) getTextStr:(NSString *)text;
@end
@property (nonatomic, unsafe_unretained) id<pickViewDelegate> delegate;//聲明代理
pickViewController.m中點(diǎn)擊事件
//確定按鈕
- (IBAction)submit:(id)sender{
if (_delegate && [_delegate respondsToSelector:@selector(getTextStr:)]) {
[_delegate getTextStr:_chooseText];
}
[self dismissViewControllerAnimated:YES completion:nil];
}
在viewcontrol.h頁面中實(shí)現(xiàn)代理
@interface ViewController : UIViewController<pickViewDelegate>
@property (nonatomic, weak) IBOutlet UILabel *showTextLb;
在viewcontrol.m頁面中接受代理傳過來的值
<pre><code>- (void)getTextStr:(NSString *)text{
_showTextLb.text = text;
}
</code></pre>
需要注意的是viewcontrol.m中的頁面跳轉(zhuǎn)這段代碼
pickViewController *pick = [[pickViewController alloc] initWithNibName:@"pickViewController" bundle:nil];
pick.delegate = self;//此處一定要實(shí)現(xiàn)自己的代理
pick.modalPresentationStyle = UIModalPresentationCustom;
[self presentViewController:pick animated:YES completion:nil];