开发者

Why Isn't My Info Button Working?

My Info button is showing up when I run my app but it doesn't do anything, no response when I click it.

In my ProfileViewController file:

- (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:UIControlEventTouchUpInsid开发者_运维问答e];
    [self.view addSubview:infoButton];

    [super viewDidLoad];
}

I also have the following two methods to load the about view (the screen that loads up when the button is clicked):

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

EDIT: I am adding my full implementation file here:

#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


Using your provided code works for me, if self.navigationController is non-nil, and there exists an AboutViewController.xib in your main bundle. Otherwise, I don't see anything readily wrong.

If you don't have a navigation controller, the proper line in toggleCreditsOpen: would be:

[self presentModalViewController:theController animated:YES];

Edit:

If you want to dismiss the modal view later, its usually a good idea to do this with a delegate. Instead of

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

you can do

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

where AboutViewController has a delegate. Probably something like id<DismissModalProtocol> delegate;. Then, your view controller would implement this @protocol, and before it calls presentModalViewController:animated:, it'll set itself as the delegate. So very roughly, it would look something like this:

// 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


I don't know at all why this isn't working, but if I were to take a guess, I'd say it's in the [self.navigationController presentModalViewController:theController animated:TRUE];

statement. From what I know, it should be:

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

See if it works after you do that. I'm not sure, but that could be the problem with your code.


hi you are took a mistake to define your button.. just assign retain property at the end of button like UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark] retain];

and also give [infoButton addTarget:self action:@selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜