gpt4 book ai didi

ios - 您如何以编程方式调用 View Controller 以在 Storyboard 中获取其 View ?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:17:32 25 4
gpt4 key购买 nike

我正在使用 BookController 类,它使用页码来跟踪当前 View 。目前我正在按需创建每个 View Controller 并以编程方式编写代码。我想访问我在 StoryBoard(xib 文件)中创建的 View Controller ,这样当我需要一个新页面时,它将访问我创建的第二个 View Controller 。

// Provide a view controller on demand for the given page number

- (id) viewControllerForPage: (int) pageNumber{

if ((pageNumber < 0) || (pageNumber > 31)) return nil;


if(pageNumber == 0){

//here is where I want to access the entire xib file that the SecondViewController is connected with
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil];
SecondViewController *myVC = (SecondViewController *)[storyboard instantiateViewControllerWithIdentifier:@"SecondViewController"];

myVC = [BookController rotatableViewController];

return myVC;

}
else if(pageNumber == 1){


// Establish a new controller
UIViewController *controller = [BookController rotatableViewController];


// Add a text view
UITextView *textview = [[UITextView alloc] initWithFrame:(CGRect){.size = CGSizeMake(100.0f,100.0f)}];
textview.text = [NSString stringWithFormat:@"This is dedicated to people"];
textview.font = [UIFont fontWithName:@"Futura" size:18.0f];
textview.center = CGPointMake(475.0f, 700.0f);
[controller.view addSubview:textview];

// Add a label
UILabel *textLabel = [[UILabel alloc] initWithFrame:(CGRect){.size = CGSizeMake(200.0f, 200.0f)}];
textLabel.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
textLabel.text = [NSString stringWithFormat:@"1"];
textLabel.font = [UIFont fontWithName:@"Futura" size:18.0f];
textLabel.center = CGPointMake(475.0f, 985.0f);
[controller.view addSubview:textLabel];


// Add it as an image
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"icon@2x.png"]];
imageView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
imageView.center = CGPointMake(160.0f, 230.0f);
[controller.view addSubview:imageView];


return controller;
}

Just not sure how to make a call to access that xib file i've created and make it into the first page (page=0). The second page (page =1) is an example of how i have drawn all the other pages in my book programmatically. Thanks!

最佳答案

记住 Storyboard 只是 NIB 的集合,它简单地实例化每个 View 的层次结构并将导出连接到拥有的 View Controller 。您不想自己实例化 Storyboard 而只创建一个 View Controller 。这样做是在应用程序已经启动并运行不同实例时创建新实例。即使您确实连接了它们,它们也会连接到冗余的实例,而不是您想要的实际实例。

我会做的是为您将单独使用的 SecondViewController 创建一个单独的 NIB 文件。然后你需要将它们连接在一起。如果这段代码在您需要访问的实例中,您只需将它传递给 SecondViewController 上的一个属性。或者您可能只是传递值,但很可能您想要设置一个委托(delegate)属性并为 SecondViewController 定义一个协议(protocol)以回调创建它的实例。

对于您的代码,您只需使用以下代码加载 NIB。

SecondViewController *vc = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];
vc.delegate = self;

您只需要定义该委托(delegate)以及可能需要的任何属性,以便将数据提供给新创建的 View Controller 。

下面是我最近使用 Storyboard 为 SideBar 界面创建的委托(delegate)设置示例。我有一个位于 Home VC 中的 Header VC 的容器 View 。这个 Header VC 可能就像你的 SecondViewController,因为我无法在 Storyboard 中连接它,所以我用代码完成了它。首先,我在 Header VC 上创建了一个委托(delegate)属性。

@protocol IFHeaderDelegate;

@interface IFHeaderViewController : UIViewController

@property (nonatomic, assign) IBOutlet id<IFHeaderDelegate> delegate;

@end

@protocol IFHeaderDelegate <NSObject>

- (void)headerViewDidToggleSideBar:(IFHeaderViewController *)sender;

@end

然后,当点击按钮时,我使用委托(delegate)进行回调。 (请注意,我使用 NSAssert 来验证委托(delegate)的定义,只是为了在我错过它时提醒我。)

#import "IFHeaderViewController.h"

@interface IFHeaderViewController ()

@end

@implementation IFHeaderViewController

- (IBAction)siderBarButtonTapped:(id)sender {
NSAssert(self.delegate != nil, @"Delegate must be defined!");

if (self.delegate != nil) {
[self.delegate headerViewDidToggleSideBar:self];
}
}

@end

但是为了连接它,我必须从 Home VC 设置代理,而我不能从 Storyboard 设置代理。当在 prepareForSegue 中触发嵌入转场时,我所做的是在 Home VC 中设置它。

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
DebugLog(@"segue.identifier: %@", segue.identifier);

if ([@"HomeHeader" isEqualToString:segue.identifier]) {
NSAssert([segue.destinationViewController isKindOfClass:[IFHeaderViewController class]], @"Destination VC must be the Header VC");
IFHeaderViewController *headerVC = (IFHeaderViewController *)segue.destinationViewController;
headerVC.delegate = self;
}
}

您可以在 GitHub 上找到完整的项目:https://github.com/brennanMKE/Interfaces/tree/master/SideBar

关于ios - 您如何以编程方式调用 View Controller 以在 Storyboard 中获取其 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13695931/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com