将视图加载到 UIScrollView 并使内部内容正常工作

发布于 2024-10-31 19:03:15 字数 2975 浏览 0 评论 0原文

我正在开发一个基于选项卡的应用程序。

我有一个名为DetailsView.m 的视图控制器,以及一个名为DetailsView.xib 的附带nib 文件。其中有几个 UILabel,它们使用 IBOutlet 链接到 DetailsView.m 视图控制器。当我使用选项卡栏控制器加载此视图控制器/视图时,它工作正常并且 UILabels 是动态填充的。

现在,我想将整个视图加载到 UIScrollView 中,以便可以容纳更多内容。

因此,我使用名为 DetailsHolder.xib 的 nib 文件创建了另一个名为 DetailsHolder.m 的视图控制器,并将其分配给选项卡栏控制器。

我在下面编写了这段代码,将第一个视图 (DetailsView) 加载到第二个视图 (DetailsHolder) 中的 UIScrollView 中。我在DetailsHolder的viewDidLoad方法中编写了它:

DetailsView* detailsView = [[DetailsView alloc] initWithNibName:@"DetailsView" bundle: nil];
CGRect rect = detailsView.view.frame;
CGSize size = rect.size;
[scrollView addSubview: detailsView.view];    
scrollView.contentSize = CGSizeMake(320, size.height);

这正确地将子视图加载到UIScrollView中,但是,DetailsView内部的标签不再执行任何操作。当我将 NSLog 放入 DetailsView 的 viewDidLoad 中时 - 它从不记录任何内容。就好像我已经加载了笔尖,但它不再与视图控制器关联。我在这里缺少什么?我是 obj C/iOS 的一个新手(但有多年的 Actionscript/Javascript 知识。

提前致谢,

Rich

Edit:按要求的 DetailsView 内容:

DetailsView.h:

#import <UIKit/UIKit.h>
#import "AppModel.h"

@interface DetailsView : UIViewController {
    IBOutlet UITextView* textView;
    IBOutlet UIImageView* imageView;
}

@end

DetailsView.m

#import "DetailsView.h"

@implementation DetailsView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    AppModel* model = [AppModel sharedInstance];

    [model loadData];

    int selectedLocation = [model getSelectedLocation];
    NSArray *locations = [model getLocations];
    NSArray *data = [locations objectAtIndex:selectedLocation];

    textView.text = [data objectAtIndex: 0];


    UIImage *theImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[data objectAtIndex: 4] ofType:@"jpg"]];

    imageView.image = theImage;

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

本质上它所做的就是获取 selectedLocation (int) 来自单例(这是我的模型),然后从模型中获取一个数组,然后尝试将图像和一些文本插入到我的视图中。在我的 nib 文件中,我有一个 UIImageView 和一个 UITextView。已链接到 DetailsView.h 中声明的两个 IBOutlet

I have a tab based application I am working on.

I have a view controller named DetailsView.m, with an accompanying nib file called DetailsView.xib. This has a couple of UILabels in, which are linked using IBOutlet to DetailsView.m view controller. When I load this view controller/view using the tab bar controller, it works fine and the UILabels are populated dynamically.

Now I want to load this entire view inside a UIScrollView instead so I can fit more content in.

So I created another view controller called DetailsHolder.m with a nib file called DetailsHolder.xib, and assigned this to the tab bar controller.

I wrote this code below to load the first view (DetailsView) into the UIScrollView in the second view (DetailsHolder). I wrote it in the viewDidLoad method of DetailsHolder:

DetailsView* detailsView = [[DetailsView alloc] initWithNibName:@"DetailsView" bundle: nil];
CGRect rect = detailsView.view.frame;
CGSize size = rect.size;
[scrollView addSubview: detailsView.view];    
scrollView.contentSize = CGSizeMake(320, size.height);

This correctly loads the sub view into the UIScrollView, however, the labels inside DetailsView no longer do anything. When I put an NSLog inside viewDidLoad of DetailsView - it never logs anything. It's as if I've loaded the nib ok, but its no longer associated with the view controller anymore. What am I missing here? I'm a bit of a newbie in obj C/iOS (but have many years Actionscript/Javascript knowledge.

Thanks in advance,

Rich

Edit: Contents of DetailsView as requested:

DetailsView.h:

#import <UIKit/UIKit.h>
#import "AppModel.h"

@interface DetailsView : UIViewController {
    IBOutlet UITextView* textView;
    IBOutlet UIImageView* imageView;
}

@end

DetailsView.m

#import "DetailsView.h"

@implementation DetailsView

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated
{
    AppModel* model = [AppModel sharedInstance];

    [model loadData];

    int selectedLocation = [model getSelectedLocation];
    NSArray *locations = [model getLocations];
    NSArray *data = [locations objectAtIndex:selectedLocation];

    textView.text = [data objectAtIndex: 0];


    UIImage *theImage = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[data objectAtIndex: 4] ofType:@"jpg"]];

    imageView.image = theImage;

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end

Essentially all its doing is grabbing selectedLocation (int) from a singleton (which is my model), then grabbing an array from the model, and then trying to insert an image and some text into my view. In my nib file, i have a UIImageView and a UITextView, which I have linked to the two IBOutlets declared in DetailsView.h

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

逆光飞翔i 2024-11-07 19:03:15

当你像这样使用视图控制器时,视图控制器中不会发生很多事件,例如viewWillAppear、viewWillDisappear和设备旋转事件。我最好的猜测是您在其中一些事件方法中进行了一些初始化。发布 DetailsView 的代码可以更轻松地找到问题。

When you use the view controller like this, many events will not occur in the view controller, e.g. viewWillAppear, viewWillDisappear and device rotation event. My best guess that you did some initialisation in some of those event methods. Posting your code for DetailsView would make it easier to find the problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文