当试图从UIView的按钮转到另一个ViewController时,代表不起作用

时间:2017-08-01 06:25:08

标签: ios objective-c uiview segue objc-protocol

我试图以编程方式转到另一个故事板上的视图控制器。

提供更多细节;我的xib上有一个UILabel和UIButton,其中UILabel包含一个" Lorem ipsum"和按钮的类型更改为自定义,以便它可以是透明的。我已将按钮的大小设置为覆盖所有xib文件。因此,当用户点击按钮时,我可以在该按钮的操作中创建一个segue。

我知道我不能直接这样做,所以我必须有一个委托方法,它将从我的xib的父视图控制器中调用。当我运行我的项目时,它一直落入Mini.m文件的btnClick动作的其他块中。

enter image description here

我也做了一些搜索,并阅读了以前关于SO的帖子,如下面的链接,但不知何故无法解决问题。我在这里缺少什么想法?

https://stackoverflow.com/a/35583877/1450201

https://stackoverflow.com/a/6169104/1450201

https://stackoverflow.com/a/30508168/1450201

https://stackoverflow.com/a/10520042/1450201

Mini.h

#import <UIKit/UIKit.h>

@protocol SelectionProtocol;

@interface Mini : UIView

@property (nonatomic, weak) id<SelectionProtocol> delegate;

- (IBAction)btnClick:(id)sender;

@end


@protocol SelectionProtocol <NSObject>

@required
-(void) isClicked;

@end

Mini.m

#import "Mini.h"

@implementation Mini

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
    if (self = [super initWithCoder:aDecoder]) {
        [self load];
    }

    return self;
}


- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self load];
    }
    return self;
}

- (void)load {
    UIView *view = [[[NSBundle bundleForClass:[self class]] loadNibNamed:@"Mini" owner:self options:nil] firstObject];
    [self addSubview:view];
    view.frame = self.bounds;

    //    ui component properties will be set here

}
- (IBAction)btnClick:(id)sender {

    if (self.delegate && [self.delegate respondsToSelector:@selector(isClicked)]) {
        [self.delegate isClicked];
    } else {
        NSLog(@"AAAAAAAAAAAAAA");
    }
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "Mini.h"

@interface ViewController : UIViewController <SelectionProtocol>

@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


-(void) isClicked {
    UIStoryboard *targetStoryBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    UIViewController *targetVC = (UIViewController *)[targetStoryBoard instantiateViewControllerWithIdentifier:@"SecondViewController"];
    [self.navigationController pushViewController:targetVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

编辑:我使用我的UIView作为我的UIViewCotnroller的子视图。这是一个scfeenshot enter image description here

编辑2: 我最新更改的屏幕截图:(仍然无法导航到SecondViewController) enter image description here

enter image description here

enter image description here

3 个答案:

答案 0 :(得分:1)

只需像这样设置委托

在ViewController.m中

- (void)viewDidLoad {

[super viewDidLoad];

Mini *view = [[Mini alloc]init];
 view.delegate = self;
// Do any additional setup after loading the view, typically from a nib.
}

答案 1 :(得分:1)

您没有将ViewController设置为delegate视图的Mini。我假设Mini视图是ViewController的子视图。您需要做的就是在ViewController类中创建此视图的引用/指针,并将其设置为视图的delegate

  1. 创建视图的插座,并将其与故事板中的视图相关联。别忘了在Xcode的身份检查器中将此视图的类更改为Mini

    #import "Mini.h"
    
    @interface ViewController : UIViewController <SelectionProtocol>
        @property (weak, nonatomic) IBOutlet Mini *miniView;
    @end
    
  2. viewDidLoad设置此视图的delegate

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.miniView.delegate = self;
    }
    

答案 2 :(得分:0)

您应该指定符合协议的实例作为代理。object.delegate = self