Question

CONFIGURATION:

-UIviewController embedded in Navigationcontroller. -UIviewController has a UIscrollview as subview -UIscrollview has some views where charts are created: each view containing a chart has its own .h and .m file and from this file I want trigger a segue to a tableview controller. -A Tableviewcontroller was added in xcode and a segue from the UIviewController to the TableViewcontroller was created as well (Xcode)

-created a protocol in the UIView to have the segue pushed from there.

PROBLEM:

delegate always nil, segue method will never be called

UIVIEW .h file

@protocol UItoUIvcDelegate <NSObject>

-(void)triggerSegue;


@end

@interface CFfirstGraph : UIView <CPTPlotDataSource , CPTPieChartDelegate,UIActionSheetDelegate>

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


@end

UIVIEW .m file (snippet)

-(void)pieChart:(CPTPieChart *)pieChart sliceWasSelectedAtRecordIndex:(NSUInteger)index
 {

     if (self.delegate == nil)
     {
         NSLog(@"nil");
     }
     [self.delegate triggerSegue];
}

UIVIEWCONTROLLER .h file

#import "CFfirstGraph.h"

@interface CFMainViewController : UIViewController <UItoUIvcDelegate, UITableViewDelegate, UITableViewDataSource>


@end

UIVIEWCONTROLLER .m file (snippet)

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.scrollView.contentSize = CGSizeMake(320, 1000);
    [self.view addSubview:self.scrollView];
    CFfirstGraph *click =[[CFfirstGraph alloc]init];
    click.delegate = self ;

}


-(void)triggerSegue

{
    [self performSegueWithIdentifier:@"detailedData" sender:self];
    NSLog(@"estoy aqui");
}

What am I doing wrong ? why the delegate is always nil ? I tried to add the method setDelegate but still no luck.

Thanks, dom

Was it helpful?

Solution 2

ok, after many hours of sweat I found the issue.

First...delegate = nil was not the main problem.

The real issue was the protocol method triggering the segue was never called.

If i create and initialize a CFfirstGraph object (or even property) it won't be related to the view created already in x-code, and this is the main issue.

On the other hand...if I "CTRL-drag" an outlet from the UIview to the CFMainViewController i will have a property that is exactly the one i need:

@interface CFMainViewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet CFfirstGraph *FirstGraph;

Then i assign the delegate to self (CFMainViewController) in the viewDidLoad:

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

    }

and the delegate method "triggerSegue" will be executed being called from the UIVIEW.

Best Regards, dom

OTHER TIPS

Make CFfirstGraph as a strong property.

@property (strong, nonatomic) CFfirstGraph * click;

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.click =[[CFfirstGraph alloc]init];
    self.click.delegate = self ;


    self.scrollView.contentSize = CGSizeMake(320, 1000);
    [self.view addSubview:self.scrollView];

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top