我在我的应用程序中使用UITabBarItem
和UIButton
时遇到问题。我的按钮位于UITabBarItem
内。当我按下它时,我想被推到另一个控制器以显示PDF。
以下是一段适用于其他情况的代码:
- (void)viewDidLoad {
UIImage* imageButton = [UIImage imageNamed:@"pdf-button.png"];
UIButton *buttonPDF = [UIButton buttonWithType:UIButtonTypeCustom];
buttonPDF.frame = CGRectMake(SCREEN_WIDTH/2 - 100, 100, 200, 36);
[buttonPDF setImage:imageButton forState:UIControlStateNormal];
buttonPDF.contentMode = UIViewContentModeScaleAspectFit;
buttonPDF.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
[buttonPDF addTarget:self action:@selector(displayPDFParams:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonPDF];
}
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = documentParametres;
[self.navigationController pushViewController:pdfController animated:YES];
}
displayPDFParams
被调用,但它并没有让我感动pdfController
。我认为这是因为我无法定位我的应用程序的父导航控制器...
任何帮助将不胜感激。提前谢谢!
答案 0 :(得分:1)
您需要使用导航控制器初始化根视图控制器。这是代码。
在 AppDelegate.h
中#import <UIKit/UIKit.h>
#import "HomeViewController.h"
@class HomeViewController;
@interface IDSAppDelegate : UIResponder <UIApplicationDelegate>{
UINavigationController *nav;
}
@property (strong, nonatomic) HomeViewController *homeViewController;
@end
在 AppDelegate.m
中#import "IDSAppDelegate.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.homeViewController = [[HomeViewController alloc] initWithNibName:@"HomeViewController" bundle:nil];
nav=[[UINavigationController alloc]initWithRootViewController:self.homeViewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
答案 1 :(得分:1)
通过在我的UIViewController中定义属性(如UITabBarItem)解决问题,如下所示:
@property (nonatomic, retain) UINavigationController *superNavController;
并在我的UITabBarController中设置:
self.myViewController.superNavController = self.navigationController;
最后我修改了displayPDFParams方法:
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = self.documentParametres;
[self.superNavController pushViewController:pdfController animated:YES];
}
完美无缺!