2010-11-26 16 views
0

我有一个viewcontroller“ResultsViewController”与一个名为emailbutton的按钮。当按下这个按钮时,我想要从另一个叫做“Illusions_AppViewController”的视图调用一个函数(这两个视图控制器都没有链接)。代表不被称为

因此,我在 “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 

在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]; 
    } 

} 

以我其它视图, “Illusions_AppViewController.m”:

- (void)resultspage{ 

    NSLog(@"Entered results page"); 
    ResultsPageController *resultspagecontroller = [[ResultsPageController alloc] initWithNibName:@"ResultsPageController" bundle:nil]; 

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

希望如果有人能帮助我这个。我不知道为什么代表不被调用。一旦我按下电子邮件按钮,应用程序就会被挂起。谢谢!

+0

请大家在今后的正确格式化您的代码的时间。 – JeremyP 2010-11-26 12:01:30

回答

-1

要回答你的第二个问题,你是在正确的轨道上。只需创建你的Illusions_AppViewController的实例,并调用illusionsView的方法时,而不是使用:

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

谢谢你的帮助......但即使如此,它也不起作用。结果页面从不显示:( – Anam 2010-11-26 08:50:21

+0

它不起作用,因为你试图从一个不再可见的视图推送视图。根据你的代码判断我在想你为什么需要在第一个时候调用Illusion_AppViewController地方?为什么不直接从你的emailButtonPressed方法推你的ResultsPageController? – Rog 2010-11-26 10:19:42

-1

或有任何其他的方式来完成这件事。我只需要在按下电子邮件按钮时调用结果页面功能。我试图用这种方式:

ResultsViewController.m

-(IBAction)emailButtonPressed:(id)sender 
    { 

NSLog(@"entered emailbuttonpressed"); 

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

现在的结果页功能被调用,但它需要显示为modalviewcontroller从未appears.the应用程序挂起的resultspagecontroller,并在没有任何错误控制台。

1

代表的实现/使用是错误的。请参阅此tutorial

希望这会有所帮助。

感谢,

Madhup