我有一个自定义组件(带有自己的Controller和自己的xib),它有一个按钮。我是使用this教程制作的。
在主ViewController中,我在ScrollView中有四个这样的组件。我希望应用程序对点击准备segue的按钮作出反应。
我无法从我的自定义组件控制器中做到这一点,因为它是一个UIView,并且无法从自定义组件连接到主ViewController,因为它不是直接的兄弟,还有一个ScrollView。因此,我无法通过Main.storyboard的View Controller中的按钮进行IBAction。
我的目标是,从自定义组件中,在Controller中为Main.storyboard创建一个IBAction,这样我就可以启动一个segue并呈现一个或另一个场景,具体取决于按下用户的按钮。
我怎么能实现这个目标?谢谢。
我的自定义组件控制器:
f(typeMap[paramsTypes[0]](unknownA), b: typeMap[paramsTypes[1]](unknownB))
My Main.storyboard的ViewController:
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()
}
}
}
答案 0 :(得分:0)
从ViewContoller(包含自定义视图)ctrl +拖动到想要在UIView上实例化的ViewController(坐在Scroll视图中)点击。
现在选择segue类型并根据需要命名。 当UIView收到轻敲电话时
[self performSegueWithIdentifier:@"segueName" sender:self];
编辑:委托方法
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