簡體   English   中英

為什么我的“信息”按鈕不起作用?

[英]Why Isn't My Info Button Working?

當我運行我的應用程序時,“我的信息”按鈕會顯示,但是它什么也沒做,單擊時無響應。

在我的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

我完全不知道為什么這不起作用,但是如果我猜測的話,我會說它在[self.navigationController presentModalViewController:theController animation:TRUE];中。

聲明。 據我所知,應該是:

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

這樣做之后,看看它是否有效。 我不確定,但這可能是您的代碼存在的問題。

嗨,您在定義按鈕時犯了一個錯誤。只需在按鈕的末尾分配保留屬性,如UIButton * infoButton = [UIButton buttonWithType:UIButtonTypeInfoDark] keep];

並給[infoButton addTarget:self action:@selector(toggleCreditsOpen:)forControlEvents:UIControlEventTouchDown];

如果self.navigationControllernil ,並且您的主捆綁包中存在AboutViewController.xib,那么使用您提供的代碼對我self.navigationController 否則,我看不到任何容易出錯的地方。

如果沒有導航控制器,則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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM