0

I'm trying to programmatically segue to a view controller which is on another storyboard.

To give more details; I've an UILabel and UIButton on my xib where UILabel contains a "Lorem ipsum" and button's type changed custom so that it can be transparent. I've set the button's size to cover all xib file. So when user tap on button, i can create a segue within that button's action.

I know i can't do this directly, so i must have a delegate method which will be invoked from my xib's parent viewcontroller. When i run my project, it keeps falling into else block of Mini.m file's btnClick action.

enter image description here

I've also done some searching and read some previous posts on SO like following links but somehow couldn't manage to solve the issue. Any ideas what am i missing here?

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

Edit: I'm using my UIView as a subview of my UIViewCotnroller. Here's a scfeenshot enter image description here


Edit 2: Screenshots for my latest changes: (Still can't navigate to SecondViewController) enter image description here

enter image description here

enter image description here

t1w
  • 1,408
  • 5
  • 25
  • 55
  • Where are you setting your `ViewController` as the `delegate` of your view? – Adeel Miraj Aug 01 '17 at 06:30
  • So is there something missing in my ViewController.h file? I thought what i did there was setting it as delegate of the view – t1w Aug 01 '17 at 06:36
  • 1
    In `ViewController.h` file you are only saying that this class implements the `SelectionProtocol` but you are not telling the instance of `Mini` class that this `ViewController` is its `delegate`. – Adeel Miraj Aug 01 '17 at 06:41
  • oh.. well, how do i do that? – t1w Aug 01 '17 at 06:43
  • Is this view a subview of the `ViewController`? Provide a bit more context of your problem. For example, when do you show this view on the screen? – Adeel Miraj Aug 01 '17 at 06:45
  • yes it is. i'm using this uiview to prepare a ui component so that i could use it multiple times in future. my uiview will be shown at the same time as it's parent view controller – t1w Aug 01 '17 at 06:47
  • Then when you add the sub view, set the custom view's delegate as self in view controller class. – jegadeesh Aug 01 '17 at 06:49
  • i've added screenshot of my ui at the end of my question. – t1w Aug 01 '17 at 06:52
  • i'm sorry i didn't know it was something to be set from interface builder. I just dragged a uiview in my viewcontroller and set it's custom class to Mini. Can you elaborate more on that? – t1w Aug 01 '17 at 06:55
  • @jegadeesh i already did that.@Adeel told me that too. Could you refer this github link where i share the project? https://github.com/TimurAykutYildirim/demoView – t1w Aug 01 '17 at 08:00

3 Answers3

1

Just set delegate like this

In 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.
}
Anbu.Karthik
  • 82,064
  • 23
  • 174
  • 143
vikram
  • 181
  • 2
  • 5
  • it didn't work. still falling into else block of Mini.m file's btnClick action – t1w Aug 01 '17 at 07:17
  • use This if (self.delegate && [self.delegate respondsToSelector:@selector(ViewController.isClicked)]) { – vikram Aug 01 '17 at 07:19
  • i'm sorry what did you mean by that? i shall change it to what exactly? i'm using it as in if block but somehow it keeps falling into else block – t1w Aug 01 '17 at 07:22
  • Update this Method - (IBAction)btnClick:(id)sender { if (self.delegate && [self.delegate respondsToSelector:@selector(ViewController.isClicked)]) { [self.delegate isClicked]; } else { NSLog(@"AAAAAAAAAAAAAA"); } } – vikram Aug 01 '17 at 07:24
  • it want's to add semicolon ":" before the dot of **ViewController.isClicked** than it gives error – t1w Aug 01 '17 at 07:32
  • Try to run [self.delegate isClicked]; without if condition it will work, Than try to debug your if condtion it seems its not confirming that protocol. – vikram Aug 01 '17 at 07:34
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150693/discussion-between-timur-aykut-yildirim-and-vikram). – t1w Aug 01 '17 at 07:38
1

You are not setting the ViewController as the delegate of Mini view. I assume that the Mini view is a subview of ViewController. All you need to do is create a reference/pointer of this view in the ViewController class and set it as the delegate of the View.

  1. Create an outlet of your view and connect it with the view placed in the storyboard. Don't forget to change the class of this view to Mini in the identity inspector of the Xcode

    #import "Mini.h"
    
    @interface ViewController : UIViewController <SelectionProtocol>
        @property (weak, nonatomic) IBOutlet Mini *miniView;
    @end
    
  2. In the viewDidLoad set the delegate of this view

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.miniView.delegate = self;
    }
    
Adeel Miraj
  • 2,472
  • 3
  • 22
  • 32
  • quick question, have you noticed anything else odd? i did try your solution but couldn't navigate to second vc – t1w Aug 01 '17 at 07:10
  • Did you connect the `IBOutlet` with the view placed in the storyboard? Also change the class of this view to `Mini` in the identity inspector. – Adeel Miraj Aug 01 '17 at 07:16
  • i created an outlet of the view component placed in my viewcontroller to ViewController.h file then called in viewDidLoad – t1w Aug 01 '17 at 07:21
  • added 3 screenshots at the end of my question – t1w Aug 01 '17 at 07:27
  • [Here's](https://www.youtube.com/watch?v=0Bgmw9gPOgI) a short video on how to connect IBOutlet to storyboard. – Adeel Miraj Aug 01 '17 at 07:36
  • thanks but as you can see from the last screenshot, i know how to create an outlet. if you could just examine the last 3 screenshots, i think you will see what's what in my code. in fact, let me share it with you by bitbucket – t1w Aug 01 '17 at 07:41
  • Where is your button on the storyboard? Did you connect it to `- (IBAction)btnClick:(id)sender`? – Adeel Miraj Aug 01 '17 at 07:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/150696/discussion-between-timur-aykut-yildirim-and-adeel). – t1w Aug 01 '17 at 08:02
  • my button is on the xib. let's continue in chat. can't keep writing long comments – t1w Aug 01 '17 at 08:03
0

You should specify a instance who conform to the protocol as the delegate.object.delegate = self.

iticle
  • 131
  • 1
  • 4