文章題目:Property for Double and CGFloat
作者:pmst(1345614869)
微博:PPPPPPMST
正文
問題描述
VolmDev 問了一個(gè)自己在實(shí)際開發(fā)中遇到的問題:應(yīng)用需要登錄操作,為此當(dāng)主界面出現(xiàn)之后依痊,跳轉(zhuǎn)到登陸界面,輸入賬號(hào)密碼差油,點(diǎn)擊 Login 按鈕殴俱,關(guān)閉登陸界面相赁,回到主界面饵蒂,一切就應(yīng)該結(jié)束,不是嗎脏款? 可問題偏偏出現(xiàn)了!當(dāng)關(guān)閉登陸界面后居然又跳出了登陸界面裤园!再關(guān)閉又打開撤师? VolmDev 抓狂了,這尼瑪什么情況拧揽!他附上了完整的代碼剃盾,如下:
這是ViewController.swift 文件源代碼:
// ViewController.swift
import UIKit
import SafariServices
class ViewController: UIViewController, LoginViewDelegate {
var safariViewController: SFSafariViewController?
override func viewDidLoad() {
super.viewDidLoad()
}
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
showPopUp()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func showPopUp()
{
let storyboard = UIStoryboard(name: "Main", bundle: NSBundle.mainBundle())
if let loginVC = storyboard.instantiateViewControllerWithIdentifier(
"LoginViewController") as? LoginViewController {
loginVC.delegate = self
self.presentViewController(loginVC, animated: true, completion: nil)
}
}
// delegate function.
func didTapLoginButton() {
self.dismissViewControllerAnimated(false, completion: nil)
// if let authURL = GitHubAPIManager.sharedInstance.URLToStartOAuth2Login() {
// safariViewController = SFSafariViewController(URL: authURL)
// safariViewController?.delegate = self
// if let webViewController = safariViewController {
// self.presentViewController(webViewController, animated: true, completion: nil)
// }
// }
}
}
這是 LoginViewController.swift 文件源代碼:
// LoginViewController.swift
// TestingPopUps
import UIKit
// this is a delegate function that can be used
// to call a funct+-*`*ion outside this class to do
// something on behafe of this class.
protocol LoginViewDelegate: class {
func didTapLoginButton()
}
class LoginViewController: UIViewController {
weak var delegate: LoginViewDelegate?
@IBAction func btnDismiss(sender: AnyObject) {
// Note: I tried dismissing here too with failure
//self.dismissViewControllerAnimated(false, completion: nil)
if let delegate = self.delegate {
delegate.didTapLoginButton()
}
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
當(dāng)然,storyboard 還有些不關(guān)緊要的東西淤袜,倘若有興趣痒谴,你可以自己來補(bǔ)充。
倘若不想看源代碼思考下铡羡,請(qǐng)直接跳轉(zhuǎn)到問題解答积蔚。
問題解答
該例出現(xiàn)的問題其實(shí)很簡單,但也是新手經(jīng)常犯的錯(cuò)誤烦周。貼出邏輯錯(cuò)誤的部分:
override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)
showPopUp()
}
當(dāng) viewController 的主視圖出現(xiàn)之后尽爆,調(diào)用showPopUp方法彈出登陸界面,貌似沒什么錯(cuò)誤读慎。此時(shí)登陸界面位于主視圖之上漱贱,輸入賬號(hào)密碼之后點(diǎn)擊按鈕,通過 protocol+delegate 方法告知 viewController 視圖控制器用戶點(diǎn)擊了登陸按鈕夭委,此時(shí)事件處理只是簡單的關(guān)閉登陸視圖:
func didTapLoginButton() {
self.dismissViewControllerAnimated(false, completion: nil)
}
關(guān)閉視圖之后幅狮,意味著 viewController 的視圖又出現(xiàn)了!那么就會(huì)調(diào)用viewDidAppear
方法株灸,再次調(diào)用了showPopUp
方法崇摄!問題就是這么來的。
思考
今天的問題很基礎(chǔ)蚂且,但是同樣有我們值得學(xué)習(xí)的地方配猫,我簡單歸納下:
- 你需要了解 ViewController 中幾個(gè)方法的調(diào)用順序,譬如:viewDidLoad() viewDidAppear() LoadView()等等
- Protocol + Delegate 的用法杏死。
- 從 StoryBoard 中加載視圖控制器泵肄。也就是
storyboard.instantiateViewControllerWithIdentifier()
。