【问题标题】:Infobutton not Displaying ModalView信息按钮不显示模态视图
【发布时间】:2012-01-09 22:19:31
【问题描述】:

当按下 infobutton 时,它不显示 ModalView

  UIBarButtonItem *infoItem = [[UIBarButtonItem alloc] 
                           initWithTitle:@"Info" 
                           style:UIBarButtonItemStyleBordered 
                           target:self
                           action:@selector(displayModalView:)]; 




- (void)displayModalView 
{

self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

self.viewController = [[Infoviewcontroller alloc] init];

UINavigationController *navigationController=[[UINavigationController alloc] init];

navigationController.navigationBar.tintColor = [UIColor brownColor];  

[navigationController pushViewController:_viewController animated:YES];

[_window addSubview:navigationController.view];

self.window.rootViewController = self.viewController;

[self.window makeKeyAndVisible];   

}

任何人都可以帮我看看这是什么问题。

非常感谢您提前帮助我

【问题讨论】:

  • 您能否向我详细说明一些细节,以便我回答:您打算在哪里使用导航栏上的UIBarButtonItem?我看到您的问题在这里,因为这个创建的按钮没有链接到您在栏上看到的按钮,所以您按下的按钮没有您在其中指定的代码。
  • 此信息按钮在 UIToolbar 上。
  • 当您说问题出在此处时能否请您解释一下,因为此创建的按钮未链接到您在栏上看到的按钮,因此您按下的按钮没有您的代码在里面指定。
  • 我正在写答案,请给我时间打字:)

标签: iphone


【解决方案1】:

在您的问题中,您没有指定如何创建对象(工具栏及其上的按钮),您是通过拖放从 Xcode 中创建它们还是从纯代码中创建它们,因此我将尝试指出常见的两种情况都有问题。

首先,我假设您正在使用 Xcode 并拖动您喜欢的组件。在这种情况下,您需要在 .h 文件中创建一个将链接到栏上的按钮的 Outlet,如下所示:

@interface yourViewController : UIViewController
{
    UIBarButtonItem *barButton;
}

@property (nonatomic, retain) IBOutlet UIBarButtonItem *barButton;

- (void) barButtonPress;

请注意,我添加了一个处理栏按钮按下的函数。现在您需要将此 Outlet 链接到条形按钮项,只需在 Xcode 中的 Connection Inspector 中显示 New Referencing Outlet 拖动到 File's Owner 框(黄色立方体)。

现在在viewDidLoad 中添加以下内容:

[barButton setTarget:self];
[barButton setAction:@selector(barButtonPress)];

此代码会将您的条形按钮链接到您想要在按下它时调用的函数。现在对于您喜欢查看 Modal 的视图,我假设您已经在 .h 文件中 #import 它,我们称之为 MyViewModal。

在按下条形按钮时将调用的函数内部:

- (void) barButtonPress
{
    MyViewModal *myViewModal = [[MyViewModal alloc] initWithNibName:@"MyViewModal" bundle:nil];
    [self presentModalViewController:myViewModal animated:YES];
}

就是这样,它将显示在模态视图中。请记住,分配新视图是根据您的需要完成的,这里我做了一个最简单的例子来说明。

更新:如果不使用 Xcode

如果你不使用 Xcode,那么你应该已经定义了一个工具栏,比如它被命名为 myToolBar。要将按钮添加到工具栏,我们使用myToolbar.items 方式,因此我们需要在添加按钮之前准备按钮及其目标。这是一个工作流程:

UIButton *infoButton = [UIButton buttonWithType:UIButtonTypeInfoLight];
[infoButton addTarget:self action:@selector(barButtonPress) forControlEvents:UIControlEventAllEvents]; //same function as above
UIBarButtonItem *btn = [[[UIBarButtonItem alloc] initWithCustomView:infoButton] autorelease];
myTool.items = [NSArray arrayWithObjects:btn, nil];

这应该为你做。

【讨论】:

  • 我没有使用界面生成器。以编程方式创建所有内容
  • 我将更新答案以显示编程方式,几分钟:)
  • 我还在为此苦苦挣扎。让我看看。但是非常感谢您花时间帮助我。欣赏它。谢谢
  • UIButton *infoItem = [[UIButton buttonWithType:UIButtonTypeInfoLight] 保留]; infoItem.frame = CGRectMake(250.0, 8.0, 25.0, 25.0); infoItem.backgroundColor = [UIColor clearColor]; [infoItem addTarget:self action:@selector(displayModalViewaction:) forControlEvents:UIControlEventTouchUpInside]; infoItem.backgroundColor = [UIColor grayColor];现在我的整个应用程序都崩溃了,说 UIButton 视图。不知道为什么我的整个应用程序崩溃了。请有任何想法。
  • 首先我们需要知道 Xcode 在崩溃时显示的错误是什么,这将指导我们去哪里。一个常见的错误是释放一个计数已经为 0 的对象或尝试使用一个死对象。这些被称为 Zombies,使用 Zombie Profile(从菜单:Product->Profile->Memory->Zombies)有很大帮助。配置文件向您显示每个对象的计数,如果检测到僵尸,它会告诉您在哪个对象上......请为我们写下 Xcode 在崩溃时向您显示的错误,以便更具体地提供想法。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
  • 1970-01-01
  • 2020-07-05
  • 2012-07-15
  • 2021-04-29
相关资源
最近更新 更多