2015-08-14 26 views
10

Üç sekmeli bir UITabBarController sahibim. Belirli bir tuşa basıldığında, kullanıcının bir kişi görüntü denetleyicisini hemen görmesini isterim (ABPersonViewController sınıfının bir örneği).PersonViewController TabBarController içinde

presentViewController() yöntemini, kullanıcı görünümü denetleyicisiyle parametre olarak kullanmak istemiyorum çünkü bu, kullanıcının sunmuş olduğu temel görünümü denetleyicisini görebileceği bir gecikme ile sonuçlanır.

Ayrıca, görünüm denetleyicisini ABPersonViewController'dan devralıyorum, çünkü Apple tarafından ayarlanmış, böylece alt sınıflara bölünemez. Bunu başarabileceğim bir yol var mı? JAL cevabı

Teşekkür:

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool { 

    let navViewController = viewController as! UINavigationController 

    // First, check to see if the view controller is the one you want to override 
    if let myViewController = navViewController.viewControllers[0] as? ThirdViewController { 

     let abpvc = ABPersonViewController() 
     abpvc.personViewDelegate = self 
     self.navigationController?.pushViewController(abpvc, animated: true) 
     return false 

    } 

    return true 
} 
+1

Lütfen sorunuza cevap kopyalamayın/dahil etmeyin. Bir cevap size yardım ederse, bunu kabul edebilir ya da kabul edebilirsiniz. Ama bir cevap soruya ait değil. – Rizier123

cevap

0

UITabBarDelegate yöntemi shouldSelectViewController kullanın: (bir resim kullanıyorsanız)

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool { 

    // First, check to see if the view controller is the one you want to override 
    if let myViewController = viewController as? ViewControllerToOverride { 
     // do something here, present a new view controller, etc. 
     return false // then return false to prevent the tab from switching 

} 

sekme çubuğu öğesini seçmek için, UITabBarItem değiştirmek image veya selectedImage. Sekmeyi seçtiğinizde yanlış döndüğünüzden, selectedImage yerine image özelliğini değiştirmeniz gerekecektir.

self.tabBar.items?.first?.image = UIImage(...) 
+0

Bu yöntemin ilk satırındaki bir kesme noktasına hiçbir zaman ulaşılmaz, bu durum aslında bir sekme çubuğu öğesine basıldığında ve yeni bir vc sunulduğunda kullanılmaz. – 5813

+0

Sekme çubuğu delegate özelliğini, bu yöntemi çağırdığınız sınıfa ayarladınız mı? – JAL

+0

“UITabBarDelegate” veya “UITabBarController” delegesinden mi bahsediyoruz? 'UITabBarDelegate' dedin, ama sadece UITabBarControllerDelegate'in bu yönteme sahip olduğu belgelerden mi geliyor? – 5813

0

Cevap temel fikri UITabBarController alt sınıf ve sekme çubuğunda bir UIButton koymak, orijinal sekme alanını örtmek ve ilgili fonksiyon çağrıları tetikleyecek o düğmeye, bir eylem eklemektir

.

Böylece, tap olayı aslında hiç bir zaman tabBarController'a değil, sadece düğmeye geçirilir. Böylece, kullanıcı asla "temel görüntü denetleyicisini görmeyecek".

UITabBarController merkezinde

func AddSampleButton(){ 
    let SampleButton = UIButton() 
    SampleButton.setTranslatesAutoresizingMaskIntoConstraints(false) 
    SampleButton.setBackgroundImage(addImage, forState: UIControlState.Normal) 

    SampleButton.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: self.view.frame.size.width/5)) 

    SampleButton.addConstraint(NSLayoutConstraint(item: addButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 50)) 

    self.view.addSubview(addButton) 

    self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: addButton, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)) 

    self.view.addConstraint(NSLayoutConstraint(item: self.view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: addButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0)) 

    SampleButton.enabled = true 
    SampleButton.addTarget(self, action: "SampleAction", forControlEvents: UIControlEvents.TouchUpInside) 
} 

func SampleAction() { 
    let abpvc = ABPersonViewController() 
    abpvc.personViewDelegate = self 
    self.navigationController?.pushViewController(abpvc, animated: true) 
} 

Referans bir düğme eklenir

Örnek Kod

örnek Swift 1.2 kod:

http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/

Bildirim

Düğmeyi görmediyseniz, UIViewContoller'ın kullanım ömrüne dikkat edin; bu, hatanın kaynağına neden olabilir.