如何从UIView子类推送或呈现UIViewcontroller或storyboard?

时间:2014-09-24 06:57:05

标签: ios objective-c uiview uiviewcontroller storyboard

我做了什么:

  1. 我正在使用scrollview查看一些UIView(s),并启用了分页。
  2. 在scrollView子视图的最后一页中,我添加了一个按钮。
  3. 我还写了一个函数,它在UIView的子类中,当我们按下那个按钮时会调用它。
  4. 当我按下按钮时,我想查看故事板。

5 个答案:

答案 0 :(得分: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];
}

答案 1 :(得分:0)

看看这个

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

答案 2 :(得分:0)

试试这个

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

答案 3 :(得分:0)

首先,您需要将Storyboard Id设置为ViewController,即“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

答案 4 :(得分:0)

希望这会有所帮助

我认为你的问题的方式是 - &gt;右键你的内部你在UIView类,里面有一个按钮,你可以通过点击它来推送或呈现一个ViewController和offcourse,你不能推动或呈现你的视图控制器。所以

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

//在你的uiview calss中运行

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

//在视图控制器中运行

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

}