从 UITabBarItem 推送到 UIViewController
Push to UIViewController from a UITabBarItem
我在应用程序中使用 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
上。我认为这是因为我无法定位我的应用程序的父导航控制器......
任何人的帮助将不胜感激。提前致谢!
您需要使用导航控制器初始化根视图控制器。这是代码。
在你的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;
}
问题通过在我的 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];
}
完美运行!
我在应用程序中使用 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
上。我认为这是因为我无法定位我的应用程序的父导航控制器......
任何人的帮助将不胜感激。提前致谢!
您需要使用导航控制器初始化根视图控制器。这是代码。
在你的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;
}
问题通过在我的 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];
}
完美运行!