【问题标题】:Push to UIViewController from a UITabBarItem从 UITabBarItem 推送到 UIViewController
【发布时间】:2015-01-15 09:15:45
【问题描述】:

我在应用程序中使用 UITabBarItemUIButton 时遇到问题。我的按钮在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。我认为这是因为我无法定位我的应用程序的父导航控制器...... 任何帮助将不胜感激。提前致谢!

【问题讨论】:

    标签: objective-c uinavigationcontroller uibutton uitabbaritem pushviewcontroller


    【解决方案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;
    }
    

    【讨论】:

      【解决方案2】:

      通过在我的 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];
      
      }
      

      完美运行!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-10
        相关资源
        最近更新 更多