开发者

Delegate not being called

开发者 https://www.devze.com 2023-01-27 14:11 出处:网络
I\'ve a viewcontroller \"ResultsViewController\" with a button called emailbutton. when this button is pressed, i want a function to be called from another view called \"Illusions_AppViewController\"

I've a viewcontroller "ResultsViewController" with a button called emailbutton. when this button is pressed, i want a function to be called from another view called "Illusions_AppViewController" (both these viewcontrollers are not linked).

Therefore i defined a protocol in the "ResultsViewController.h":

 @protocol ResultsViewDelegate <NSObject>
 @optional 
 - (void) resultspage;

 @end

 @interface ResultsViewController : UIViewController
 {
     id<ResultsViewDelegate> mydelegate;
     UIButton *emailButton;
 }
 @property(nonatomic,retain) IBOutlet UIButton *emailButton;
 @property (nonatomic,assign) id<ResultsViewDelegate> mydelegate;
 @end

In the ResultsViewController.m :

-(IBAction)emailButtonPressed:(id)sender
{

    NSLog(@"entered emailbuttonpressed");// the app enters this method and gets hanged

    if ([mydelegate respondsToSelector:@selector(resultspage)]) {
        NSLog(@"entered respondstoselector");// this is never displayed in the log-showing that the delegates doesnt respond to selector
        [mydelegate resultspage];
    }

}

In my other view, "Illusions_AppViewController.m":

- (void)resultspage{

    NSLog(@"Entered re开发者_运维知识库sults page");
    ResultsPageController *resultspagecontroller = [[ResultsPageController alloc] initWithNibName:@"ResultsPageController" bundle:nil];

    resultspagecontroller.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    [self presentModalViewController:resultspagecontroller animated:YES];
}   

Would appreciate if anyone can help me with this. I've no clue of why the delegate is not called. the app gets hanged as soon as i press the emailbutton. Thanks!


The implementation/use of delegates is wrong. Please refer to this tutorial.

Hope this helps.

Thanks,

Madhup


or is there any other way to get this done. i just need the results page function to be called whenever the email button is pressed. i tried using this way:

ResultsViewController.m

   -(IBAction)emailButtonPressed:(id)sender
    {

NSLog(@"entered emailbuttonpressed");

illusions_AppViewController *illusionsview = [[illusions_AppViewController alloc]init];
[illusionsview performSelector:@selector(resultspage)];
}

Now the results page function gets called, but the resultspagecontroller that it needs to display as a modalviewcontroller never appears.the app hangs, and no errors in the console either.


To answer your second question, you are on the right track. Simply create an instance of your Illusions_AppViewController and call the illusionsView method in it instead using:

- (IBAction)emailButtonPressed {
   illusions_AppViewController *illusionsview = [[illusions_AppViewController alloc]init];
   [illusionsview resultspage];
   [illusionsview release];
}
0

精彩评论

暂无评论...
验证码 换一张
取 消