为什么我的信息按钮不起作用?

时间:2011-03-19 05:01:57

标签: iphone objective-c

当我运行我的应用程序时,

我的信息按钮显示但它没有做任何事情,当我点击它时没有响应。

在我的ProfileViewController文件中:

- (void)viewDidLoad
{
    UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark];
    infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
    [infoButton addTarget:self action:@selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:infoButton];

    [super viewDidLoad];
}

我还有以下两种方法来加载about视图(单击按钮时加载的屏幕):

- (IBAction) toggleCreditsOpen:(id)inSender
{   
    UIViewController *theController = [[UIViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
    [self.navigationController presentModalViewController:theController animated:TRUE];
}

- (IBAction) toggleCreditsClosed:(id)inSender
{
    [self.navigationController dismissModalViewControllerAnimated:TRUE];
}

编辑: 我在这里添加完整的实现文件:

#import "ProfileViewController.h"
#import "AboutViewController.h"

@implementation ProfileViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (IBAction)toggleCreditsOpen:(id)inSender
{   
    UIViewController *theController = [[UIViewController alloc] initWithNibName:@"AboutViewController" bundle:nil];
    [self.navigationController presentModalViewController:theController animated:YES];
}

- (IBAction)toggleCreditsClosed:(id)inSender
{
    [self.navigationController dismissModalViewControllerAnimated:TRUE];
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    UIButton *infoButton = [[UIButton buttonWithType:UIButtonTypeInfoDark] retain];
    infoButton.frame = CGRectMake(290.0, 10.0, 15.0, 15.0);
    //[infoButton addTarget:self action:@selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchUpInside];
    [infoButton addTarget:self action:@selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:infoButton];

    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

3 个答案:

答案 0 :(得分:0)

我根本不知道为什么这不起作用,但是如果我猜测,我会说它在[self.navigationController presentModalViewController:theController animated:TRUE];

语句。据我所知,它应该是:

[self.navigationController presentModalViewController:theController animated:YES];

执行此操作后查看是否有效。我不确定,但这可能是你的代码的问题。

答案 1 :(得分:0)

喜 你犯了一个错误来定义你的按钮.. 只需在按钮末尾指定retain属性 喜欢 UIButton * infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark] retain];

并且还给   [infoButton addTarget:self action:@selector(toggleCreditsOpen :) forControlEvents:UIControlEventTouchDown];

答案 2 :(得分:0)

如果self.navigationController不是nil,并且主捆绑中存在AboutViewController.xib,则使用您提供的代码对我有用。否则,我看不出任何容易出错的事。

如果您没有导航控制器,toggleCreditsOpen:中的正确行将是:

[self presentModalViewController:theController animated:YES];

修改

如果您想稍后关闭模态视图,通常最好使用委托来执行此操作。而不是

UIViewController *theController = [[UIViewController alloc] initWithWhatever];

你可以做到

AboutViewController *theController = [[AboutViewController alloc] initWithWhatever];

其中AboutViewController有一个委托。可能类似于id<DismissModalProtocol> delegate;。然后,您的视图控制器将实现此@protocol,并在调用presentModalViewController:animated:之前,它将自己设置为委托。所以非常粗略地说,它看起来像这样:

// AboutViewController.h
@protocol DismissModalProtocol;

@interface AboutViewController : UIViewController {
  id<DismissModalProtocol> delegate;
}
@property id<DismissModalProtocol> delegate;
@end

@protocol DismissModalProtocol <NSObject>
- (void)dismissController:(UIViewController *)viewController;
@end


// ProfileViewController.h
#import "AboutViewController.h"

@interface ProfileViewController : UIViewController <DismissModalProtocol>
@end


// ProfileViewController.m
@implementation ProfileViewController
- (void)dismissController:(UIViewController *)viewController {
  [self dismissModalViewControllerAnimated:YES];
}

- (void)toggleCreditsOpen:(id)sender {
  AboutViewController *controller = [[AboutViewController alloc] init];
  controller.delegate = self;
  [self presentModalViewController:controller animated:YES];
}
@end