2015-09-28 49 views
0

我得到了一個自定義組件,(帶有自己的控制器和它自己的xib),它有一個按鈕。我使用this教程做了它。如何從ScrollView中的自定義組件中使用prepareForSegue?

在主ViewController中,我有四個這樣的組件,在ScrollView中。我希望應用程序對點擊按鈕做出反應來準備賽季。

我不能從我的自定義組件控制器,因爲它的UIView,並不能從自定義組件連接到主ViewController,因爲它不是一個直接的兄弟姐妹,有一個ScrollView。所以,我無法從Main.storyboard的視圖控制器中的按鈕創建一個IBAction。

我的目標是,從自定義組件中,在Main.storyboard的Controller中創建一個IBAction,這樣我就可以啓動一個segue並呈現一個場景或另一個場景,具體取決於按下哪個按鈕的用戶。

我怎麼能做到這一點?謝謝。

我的自定義組件控制器:

import UIKit 

    @IBDesignable class CustomView: UIView { 

    // Our custom view from the XIB file 
    var view: UIView! 

    @IBOutlet weak var label: UILabel! 
    @IBOutlet weak var icono: UIImageView! 
    @IBAction func onClick(sender: UIButton) { 
     println("button \(label.text!)") 
     switch label.text!{ 
      case "Text 1": 
       println("First component") 

      case "Text 2": 
       println("Second component") 
      case "Text 3": 
       println("Third component") 
      case "Text 4": 
       println("Fourth component") 
      default: 
       println("Error") 
     } 
    } 
    @IBInspectable var image: UIImage? { 
     get { 
      return icono.image 
     } 
     set(image) { 
      icono.image = image 
     } 
    } 

    @IBInspectable var text: String? { 
     get { 
      return label.text 
     } 
     set (text){ 
      label.text=text 
     } 
    } 

    func xibSetup() { 
     view = loadViewFromNib() 

     // use bounds not frame or it'll be offset 
     view.frame = bounds 

     // Make the view stretch with containing view 
     view.autoresizingMask = UIViewAutoresizing.FlexibleWidth | UIViewAutoresizing.FlexibleHeight 
     //self.view.backgroundColor = UIColor.purpleColor().colorWithAlphaComponent(0) 
     self.view.backgroundColor=UIColor.clearColor() 
     // Adding custom subview on top of our view (over any custom drawing > see note below) 
     addSubview(view) 

    } 

    func loadViewFromNib() -> UIView { 

     let bundle = NSBundle(forClass: self.dynamicType) 
     let nib = UINib(nibName: "CustomView", bundle: bundle) 
     let view = nib.instantiateWithOwner(self, options: nil)[0] as! UIView 

     return view 
    } 
    /* 
    // Only override drawRect: if you perform custom drawing. 
    // An empty implementation adversely affects performance during animation. 
    override func drawRect(rect: CGRect) { 
     // Drawing code 
    } 
    */ 

    override init(frame: CGRect) { 
     // 1. setup any properties here 

     // 2. call super.init(frame:) 
     super.init(frame: frame) 

     // 3. Setup view from .xib file 
     xibSetup() 
    } 

    required init(coder aDecoder: NSCoder) { 
     // 1. setup any properties here 

     // 2. call super.init(coder:) 
     super.init(coder: aDecoder) 

     // 3. Setup view from .xib file 
     xibSetup() 
    } 

} 

我Main.storyboard的視圖控制器:

import UIKit 

class ViewController: UIViewController { 


@IBOutlet weak var customViewVirtual: CustomView! 
@IBOutlet weak var customViewContactar: CustomView! 
@IBOutlet weak var customViewPresenciales: CustomView! 
@IBOutlet weak var customViewCalendario: CustomView! 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.view.backgroundColor = UIColor(patternImage: UIImage(named: "fondo")!) 
    customViewVirtual.backgroundColor=UIColor.clearColor() 
    customViewContactar.backgroundColor=UIColor.clearColor() 
    customViewPresenciales.backgroundColor=UIColor.clearColor() 
    customViewCalendario.backgroundColor=UIColor.clearColor() 



    // Do any additional setup after loading the view, typically from a nib. 
} 


override func didReceiveMemoryWarning() { 
    super.didReceiveMemoryWarning() 
    // Dispose of any resources that can be recreated. 
} 

}

+0

您的自定義組件控制器如何是一個UIView? UIViewController在哪裏拿着你的UIView? –

+0

我做了一個xib,並製作了一個ViewController來控制它。然後,在Main.storyboard中,我添加了四個視圖,使該視圖的類成爲我的自定義組件的控制器,這是UIView的一個實例。我添加代碼的問題。 – Fustigador

回答

0

從您的ViewContoller(含自定義視圖)Ctrl +拖動到你想要的視圖控制器在UIView上實例化(坐在滾動視圖內)點擊。

現在選擇賽格類型並按照您的意願命名。 當UIView的接收水龍頭呼叫

[self performSegueWithIdentifier:@"segueName" sender:self]; 

1

enter image description here

編輯:委託方法

CustomView.h

@protocol ButtonClickDelegate <NSObject> 
-(void)didClickButton; 
@end 

@interface CustomView : UIView 
@property(nonatomic, assign) id<ButtonClickDelegate> delegate; 
@end 

CustomView.m

@implementation CustomView 

- (IBAction)buttonClicked:(id)sender { 

    //check if delegate implemented the method 
    if ([self.delegate respondsToSelector:@selector(didClickButton)]) 
    { 
     //informing the delegate about action 
     [self.delegate didClickButton]; 
    } 
} 

@end 

ContainingViewController.h - 確認到ButtonClickDelegate

@interface ContainingViewController : UIViewController<ButtonClickDelegate> 

@end 

ContainingViewController.m

@interface ContainingViewController() 
@property(nonatomic, strong) CustomView * customView; 
@end 

@implementation ContainingViewController 

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    //instantiate customView 
    //set the delegate as self 
    self.customView.delegate = self; 
} 

#pragma mark - ButtonClickDelegate method 
-(void)didClickButton 
{ 
    //custom view button was clicked 
    //performing the segue 
    [self performSegueWithIdentifier:@"segueName" sender:self]; 
} 


@end 
+0

問題是...我在我的Main.storyboard中有四個自定義組件(每個都有自己的按鈕)。所以,根據用戶按下的按鈕,我需要準備一個不同的場景。而且我無法從自定義組件連接到Main.storyboard的ViewController來進行IBAction。並且不能從UIView中調用performSegueWithIdentifier,因爲它是一個UIViewController方法。 – Fustigador

+0

將包含UIViewController的自定義視圖的segues連接到所有可能的視圖控制器,您想要實例化並唯一命名這些segues。取決於按鈕標籤或其他東西執行所需的賽格。 –

+0

誰包含您的四個自定義組件? –

相關問題