UIImageView not displaying image when property is set too early - iphone

I have an image I want to display inside a UIView. In Interface Builder, the UIView is the root and a UIImageView is its child.
The view is connected to view controller's view outlet, and the image view is connected to the image view outlet:
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
If I try to set the image property of UIImageView before it's visible, the image doesn't show up.
TestView *testView = [[TestView alloc] initWithNibName:#"TestView" bundle:nil];
testview.imageView.image = [logos objectAtIndex:indexPath.row];
[self.navigationController pushViewController:testView animated:YES];
If, however, I pass the image to the controller and set the image property in viewDidLoad, the image becomes visible.
TestView *testView = [[TestView alloc] initWithNibName:#"TestView" bundle:nil];
testview.image = [logos objectAtIndex:indexPath.row];
[self.navigationController pushViewController:testView animated:YES];
- (void)viewDidLoad
{
[super viewDidLoad];
imageView.image = image;
}
What is causing the image to not show up in the first scenario?

The iOS documentation for a UIViewController's initWithNibName:bundle: method states that:
…
The nib file you specify is not loaded right away. It is loaded the first time the view controller’s view is accessed. If you want to perform additional initialization after the nib file is loaded, override the viewDidLoad method and perform your tasks there. …
A consequence of the nib/xib file not being loaded immediately (i.e. it uses so called lazy loading) means that the imageView IBOutlet property in your TestView controller is still nil when you try to set it in your first example.
In your second example you are setting it in the viewDidLoad which is called after the nib/xib file is loaded, and therefore it works as you expect.

Related

ScrollView won't scroll after adding as subview

So i'm adding UIView with UIScrollView in it to another view by pressing segment tool:
UIView.h
#interface gettingELORolesViewController : UIViewController{
UIViewController *currentController;
NSMutableArray *viewControllers;
}
- (IBAction)SegmentToggle:(UISegmentedControl *)sender;
#property (strong, nonatomic) IBOutlet UIView *containerView;
UIView.m
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Storyboard_iPhone"
bundle:nil];
firstView *FirstView = [sb instantiateViewControllerWithIdentifier:#"firstOne"];
secondView *SecondView = [sb instantiateViewControllerWithIdentifier:#"secondOne"];
viewControllers = [NSMutableArray arrayWithObjects:FirstView,SecondView,nil];
currentController = FirstView;
[containerView addSubview:FirstView.view];//containerView = self.view
and then depends on what segment is chosen, it shows different view:
- (IBAction)SegmentToggle:(UISegmentedControl *)sender {
UIViewController *selectedView=nil;
if (sender.selectedSegmentIndex==0) {
selectedView= [viewControllers objectAtIndex:0]; // retrieve object at index 0 from viewControllers array
}
else if(sender.selectedSegmentIndex==1){
selectedView= [viewControllers objectAtIndex:1]; // retrieve object at index 1 from viewControllers array
}
[currentController.view removeFromSuperview]; // remove Current displaying view from superView
currentController=selectedView; // make selected View to be the current View
[containerView addSubview:selectedView.view]; // Add newly selected view on container View
}
secondView is tableViewController and everything is working great when i choose second segment. When i choose first one it shows up but scroll doesn't work and i don't know why.
firstView.m viewDidLoad method looks like this:
-(void)viewDidLoad{
[super viewDidLoad];
mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[mainScrollView setContentSize:CGSizeMake(320, 600)];
[mainScrollView setScrollEnabled:YES];
}
Paging is enabled in storyBoard. And it hooked up with .h file of firstView.
Please tell me what am i doing wrong.
When you call -SegmentToggle:,
you create a local variable called selectedView
you assign a view controller to selectedView depending on what segment is selected
the method ends, selectedView goes out of scope
In short, you're not doing anything. You should, at least, replace a view in the current view hierarchy with the view of the selected controller (and maybe stash a reference to the selected controller in an instance variable for later lookup?)
But taking a step back, it looks like you're trying to create a view controller that manages other view controllers. There are now very specific rules around that as of iOS 5. Please watch the WWDC 2011 video called "Implementing UIViewController Containment" for more information and examples.

Loading another NIB into one subview of a UIVIEW

I have a simple question that I couldn't see it answered on the whole site,
One XIB file that has a UIView as the main,
in it has another UIView that's connected to one IBoutlet so I can assign it later to load a view from another XIB file. That assignment doesn't work.. it remains blank...
#interface Subclass : UIViewController {
UIView *view1;
}
#property (nonatomic,retain) IBOutlet UIView *view1;
#end
i linked view1 to the subview in the xib file
in implementation in initWithNibName I'm loading Other XIB file and use it's view and assigning it to the view1. Still this doesn't work...
detailControler = [[XibViewControler alloc] initWithNibName:#"XibViewControler" bundle:nil];
//one aproach
[self.view1 addSubview: detailControler.view];
//another aproach
[self setView1:detailControler.view];
If I'm adding the subview programaticaly to [self.view addSubview:detailControler.view] and set a frame to it, it will go fullscreen, and hide the original view.
I also had a different approach that didn't work
How to load multiple views on each button tap when using a UISegmentedVIew
This is how I usually set up a UIView inside another view, although I'm not entirely sure if it's best practice or anything like that:
if(newViewController == nil){
newViewController = [[NewViewController alloc] initWithCoder:nil];
}
newViewController.view.autoresizingMask = UIViewAutoresizingNone;
if([newViewController.view superview] == nil){
[view1 addSubview:newViewController.view];
}
Hope that Helps!

Create a custom UIView as a tableHeaderView programmatically

I want to create a 'detail view' in my navigation-based app similar to the address book app.
I want to create a UIView that has an UIImageView and a UILabel that I can pass to a UITableVIew's tableHeaderView property when pushed by a navigation controller. The label text and image must take information from the managed object context when it loads.
I started trying to do this using IB, but go fed up when the label text wouldn't update, regardless of where I put the myView.UILabel.text = #"some new text". It just kept presenting the text I entered in the IB inspector window.
So without using IB, how would I go about creating a UIView that has a UILabel and a UIImageView in it?
Should I be creating a new class that is a sub-class of UIViewController or just UIView?
How do I go about creating the sub-views for the label and image?
Where should the text and image URL be set in code? (viewDidLoad or loadView or viewWillLoad)(UIView, UIViewController, detailViewController)?
If you started using IB and the UILabel text wouldn't update, sounds like the IBOutlet of your view controller isn't correctly connected to the label in IB.
Hopefully that will fix your problem with using IB. And in the viewController code, you should update that text in viewDidLoad.
But if you want to do this programmatically, I would make a subclass of UIView (not UIViewController).
And I would override initWithFrame and do all the setup there.
Something like the following:
myView.m
#implementation myView
#synthesize imageView, myLabel;
- (id) initWithFrame:(CGRect) frame
{
if (self = [super initWithFrame:frame]) {
// Setup your image view and add it to the view.
self.imageView = [[[UIImageView alloc] initWithFrame:initWithFrame:frame] autorelease];
self.imageView.image = ...
[self addSubview:self.imageView];
// Setup your label
self.myLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
self.myLabel.text = #"whatever you like";
[self addSubview:self.myLabel];
}
return self;
}
Make sure to clean up memory properly in the dealloc method of course depending on whether you like make class properties vs class variables.

Can't display UIImage in UIImageView

i want to display an UIImage within a UIImageView but it doesn't work and i don't know why.
could you please help me.
i am using the following coding:
-(void) showCorrectedImage {
MyAppDelegate *delegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
CorrectedImageController *imageController = [[CorrectedImageController alloc] initWithNibName:#"CorrectedImageView" bundle:nil];
UIImageView *imgView = [[UIImageView alloc] initWithImage:originalImage];
imageController.imageView = imgView;
[imgView release];
[delegate.navController pushViewController:imageController animated:YES];
}
There is a xib file called "CorrectedImageView.xib" and within the xib i have placed an UIImageView. I have connected the UIImageView outlet with the image view and the view outlet with the view.
original Image is defined as UIImage and initialized with a file.
br.,
martin
In these lines
UIImageView *imgView = [[UIImageView alloc] initWithImage:originalImage];
imageController.imageView = imgView;
[imgView release];
you're creating a new UIImageView instance, when you already have one in the XIB file. If your connections in IB are good, this should be all you need to do:
imageController.imageView.image = originalImage;
Also, please don't forget to release imageController after you push it like this:
[imageController release];
P.S. As to why your code doesn't work, the newly created UIImageView instance, while it replaces the previous one in the view controller, it's not added as a subview of the view controller's view. So after your code executes, you have:
An UIImageView instance retained as a subview of the main view.
Another UIImageView instance initialized with your image and retained in the view controller, but not a subview of the main view, and therefore not displayed.
sorry, but i don't get it running. maybe my nib file is wrong.
it looks as follows:
-File's Owner (CorrectedImageController)
-First Responder (UIResponder)
-View (UIView)
--ImageView (UIImageView9
I've connected the File's Owner view property to the View Element
and the File's Owner imageView property to the ImageView Element.
Did i miss something ?
BR,
Martin

Sharing image between two view

I am trying to learn something basic and very important to develop decent iPhone apps. But I don't know enough to understand what I am not getting. Here's the question:
I have a window project, with 2 views - View1, View2. On each view (thru IB) I dropped an imageView control. When I call a function in view1 I want to set the image control (show an image) of View2.
How do I do that?
There must be a simple way but I haven't managed (and did search a lot) to find a straightforward simple way to do it or at least understand the concept.
Thanks in advance.
-mE
You don't necessarily need to share the image. Is it large? Are you downloading it? In other words, does it really need to be shared?
If it does, you can just instantiate the UIImage in your app delegate and pass it to each view controller when they are created. Set a property for each view controller for the image. Something like this:
ViewController1 *controller1 = [[ViewController1 alloc init];
[controller1 setImage:image];
[[self navigationController] pushViewController:controller1];
[controller1 release];
ViewController2 *controller2 = [[ViewController2 alloc init];
[controller2 setImage:image];
[[self navigationController] pushViewController:controller2];
[controller2 release];
Then in each view controller's viewDidLoad, you can set the image for your imageView's
- (void)viewDidLoad;
{
[super viewDidLoad];
[[self imageView] setImage:[self image]];
}
I'm not sure how you're a loading your view controllers, but the above code assumes you are using a navigation controller stack. If you need clarification for a different approach, let me know.
You should connect the UIImageViews in IB to outlets in your ViewController class. In your class's .h interface file, add as appropriate:
IBOutlet UIImageView *view1;
add a property:
#property (nonatomic,retain) IBOutlet UIImageView *view1;
and in your class implementation file (.m), synthesize the property:
#synthesize view1;
Finally, you need to connect the UIImageView object in IB to that new outlet. Then you can access the image property of the UIView within your class with view1.image