开发者

Loading a view into a UIScrollView AND getting the content inside to work

开发者 https://www.devze.com 2023-02-23 13:11 出处:网络
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

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 =开发者_StackOverflow中文版 [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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号