【问题标题】:How to push or present UIViewcontroller or storyboard from UIView subclass?如何从 UIView 子类推送或呈现 UIViewcontroller 或故事板?
【发布时间】:2014-11-18 13:37:10
【问题描述】:

我做了什么:

  1. 我正在使用滚动视图查看一些UIView(s),并启用了分页。
  2. 在 scrollView 子视图的最后一页中,我添加了一个按钮。
  3. 我还在UIView 的子类中编写了一个函数,当我们按下该按钮时会调用该函数。
  4. 我想在按下该按钮时查看情节提要。

【问题讨论】:

  • 你想从你的故事板中展示 viewController 吗?还是您的 xcode 中存在的整个故事板?请纠正我的理解。
  • 我想展示我的故事板或 viewController ..来自 UIView 子类。在 UIView 子类中,我在 drawRect 的帮助下添加了一个按钮:现在当我按下该按钮时,我想要我的故事板或我眼前的 UIViewcontroller ......提前谢谢。

标签: ios objective-c uiview uiviewcontroller storyboard


【解决方案1】:

您需要为自定义 UIView 类实现协议方法。

例如;

#import <UIKit/UIKit.h>

@protocol YourCustomViewDelegate <NSObject>

- (void)buttonTapped;

@end

@interface YourCustomView : UIView

@property (nonatomic, strong) id< YourCustomViewDelegate > delegate;

@end

而.m文件可以调用buttonTapped委托方法。

- (void)myCustomClassButtonTapped:(id)sender
{
    [self.delegate buttonTapped];
}

为了正常工作;

  • 在自定义视图控制器中将自定义视图委托设置为 self。
  • 将 buttonTapped 方法添加到该视图控制器
  • 然后用那个方法做你想做的事。呈现/推送视图控制器,就像解释的其他答案一样。

编辑

我将尝试说明协议的非常简单的用法,希望对您有所帮助。

#import "MyViewController.h"
#import "YourCustomView.h"

@interface MyViewController ()<YourCustomViewDelegate> // this part is important

@end

@implementation MyViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    YourCustomView *customView = // initialize your custom view, I suppose that you already did it
    customView.delegate = self;

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buttonTapped
{
    YOUR_VIEW_CONTROLLER_CLASS *viewCon =[storyboard instantiateViewControllerWithIdentifier:@"mainVC"];    //mainVC is just a example and u will have to replace it with your viewController storyboard id

    //Pushing VC
    [self.navigationController pushViewController:viewCon animated:YES];
}

【讨论】:

  • 嘿,感谢您的宝贵回复...但我不熟悉协议...您能否详细回答如何在自定义视图控制器中将我的自定义视图委托设置为自我...以及如何将 buttonTapped 方法添加到 viewController 以及如何修改 buttontapped 方法..提前感谢
  • 我试过了..首先我只是在点击按钮时放了一个 NSLog..当我点击那个按钮时,我什至无法在日志中打印它
  • 您确定将委托设置为 customView.delegate = self; 吗? @Lalli_Mann 如果您将委托设置为 self 并且当然导入该委托,则应该可以工作。(MyViewController ())
  • 是的,我在 viewDidLoad 中添加了 mycustomview.delegate=self ...imported mycustomdelegate ..可能是我的操作有问题..谢谢
  • 我认为 ..viewDidLoad 没有被调用,这就是为什么我们无法将委托分配给 self
【解决方案2】:

看看这个

    YourViewController *obj=[[YourViewController alloc]init];
    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:obj];
    [[[UIApplication sharedApplication]keyWindow].rootViewController presentViewController:nav animated:YES completion:NULL];

【讨论】:

    【解决方案3】:

    这样试试

    UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
    UIViewController* initialView = [storyboard instantiateInitialViewController];
    

    【讨论】:

      【解决方案4】:

      首先,您需要为您的 ViewController 设置一个故事板 ID,即“mainVC”。然后你可以使用下面的代码来推送/展示它:-

       YOUR_VIEW_CONTROLLER_CLASS *viewCon =[storyboard instantiateViewControllerWithIdentifier:@"mainVC"];    //mainVC is just a example and u will have to replace it with your viewController storyboard id
      
      //Pushing VC
      [self.navigationController pushViewController:viewCon animated:YES];
      //OR presenting VC
      [self presentViewController:viewCon animated:YES completion:nil];
      

      另外,如果您希望在下面启动另一个故事板,

      UIStoryboard* storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];   //Change MainStoryboard with your storyboard id
      

      【讨论】:

        【解决方案5】:

        希望这会有所帮助

        我对你的问题的看法是 -> 现在你的内部你在UIView 类中,里面有一个按钮,通过单击它你想要推送或呈现一个 ViewController 并且从你的 UIView 类内部你不能推送或呈现你的视图控制器。所以

        你可以先在 action 方法中做一个简单的事情做一个 ref(前向声明),或者你可以创建一个 ViewController 类的全新对象,其中你的 UIView 现在存在,然后调用一个函数(它存在于你的ViewController) 喜欢

        //在你的 uiview 类中的函数

        - (void)btnaction
        {
             [_objCSInformationViewController pushViewController];
        } 
        

        // 视图控制器内部的函数

        -(void)pushViewController
        {
        yourViewController *objViewController = [[yourViewController alloc]initWithNibName:@"yourViewController" bundle:nil]; 
         [strong.navigationController pushViewController:objViewController animated:YES];
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2013-07-04
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多