do I need to initialize the new view? - objective-c

I admit I'm a newby to Objective-C but came pretty far, I guess. Now I'm having an issue and would need some help.
In my app I display images in portrait modes amongst other information and want to display them full screen in landscape mode. I learned that you'd do this defining a Landscape m, h and nib file and load this view when the device is moved to landscape orientation (working with an observer), right?
In portrait mode the images in the end depend on user interaction so the Landscape class needs to be told, which image to display. I created a putImage method in the Landscape class
-(void) putImage:(NSString *)theImage {
pict.image = [UIImage imageNamed: theImage];
NSLog(#"PICT %#",pict.image);
NSLog(#"IMAGE %#",theImage);
}
and call it from the portrait one and here comes the issue:
NSString *actualImage = [NSString stringWithFormat:#"Picture%i.jpg",selected];
pict.image = [UIImage imageNamed: actualImage];
[landscapeViewController putImage:actualImage];
that works perfectly every time except the very first one. So whatever I do I need to first make the App load a new image plus change to landscape orientation (being displayed no image) in any manner. Then after it always works displaying the correct images.
I added debug information and see that the correct image is assigned to pict in landscape class - however it seems it needs to be displayed before it really sets the image.
Any help you guys could give?

Not sure if this it it since your example code is pretty sparse. But when you load your first picture do you do that in init? If so, try to do it in viewWillAppear or viewDidAppear.

thank you for all the hints and tries to help. Now I found the solution myself ... here's what I did:
1) in the LandscapeViewController.h I defined an NSString:
#interface LandscapeViewController : UIViewController
{
IBOutlet UIImageView *pict;
NSString *actualImage;
}
2) I changed the putImage method to only assign the image name to my local NSSTRING variable:
-(void) putImage:(NSString *)theImage {
actualImage = theImage;
[actualImage retain];
}
3) loading the image I only do in viewWillAppear:
- (void)viewWillAppear:(BOOL)animated {
pict.image = [UIImage imageNamed: actualImage];
[super viewWillAppear:animated];
}
So the answer to my question is almost answered - I guess that this issue arose because I did set the image before I loaded the view first time so that it was not instanciated. I was able to call the method but had no Image Object to set ... does that sound logical to you guys?
Thank you all again!

Related

Changing the image in UIimageView

Hi I am new to ios programming,
I am trying to change the image of UIImageView outletImage
- (void)viewDidLoad
{
[super viewDidLoad];
dragView *dragViewCopy=[[dragView alloc]init];
newPic=[dragViewCopy imageChanger];
outletImage.image=[UIImage imageNamed:(NSString*)newPic];
NSLog(#"vc %#",newPic);
}
but it is not changing,inspite that the correct name of the image appears when i test it with NSLog
when i try this
outletImage.image=[UIImage imageNamed:(NSString*)#"bird.png];
it works
Can anybody help me about this
thanks
Your problem appears to be that you're trying to load an image from disk (or whatever the iOS device's hard drive equivalent is) and the actual image you have (which comes from the "imageChanger") method is entirely within memory.
So instead of doing "[UIImage imageNamed:]", you may want to create your image using "[UIImage alloc] initWithData:" or use whatever actual object type "imageChanger" returns.

set retina image in uiimageview

today i first run my app on my iphone but all my images (my background image too) were blurry, i read in the apple documentation and i understand that i should use a double pixel size (640X960 instead of 320X460). to set my image background i just use an uiiamgeview and set the image in the xib file. now, my question is how can i set the image from the .m file and not from my xib file. when i try to set the image from the .m file my image was much bigger then my uiimageview (i see only part from the image)?
thanks!
When using retina double sized images you don't actually have to do anything as far as loading them in code or in Interface Builder.
You just need to name the properly and make sure they're in your Xcode project.
For example if you had one image.png you'd need an image#2x.png in your Xcode project as well. iOS will automatically use the correct when when displaying on a Retina Device.
Update: In response to your comment.
This will create an UIImageView and set it to the size of the view. Its very similar to how most people do it in Interface Builder.
- (void)viewDidLoad
{
UIImageView *background = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"background.png"];
[background setFrame:CGRect(0.0,0.0,self.view.frame.size.width,self.view.frame.size.height)];
[self.view addSubview:background];
}

Trouble Setting Frames of NSView

I have no idea why this code isn't working, but hopefully the fix is simple, I hope someone can help.
I have 2 functions, enterFullScreen and exitFullScreen.
In the enterFullScreen method, I set an NSView's frame to the Window's frame, which has a full-screen effect, this is what I want. Within this method I store the old frames so that in the exitFullScreen method I can set them back, but this isn't working. It has no effect whatsoever. The code is as follows:
NSView oldView, oldViewContainer;
- (void) enterFullScreen:(NSView*)newView
{
// Store original views
oldView = newView;
oldViewContainer = _newViewContainer;
// Set new views to fit window
[_newViewContainer setFrame:[_window.contentView frame]];
[newView setFrame:[_window.contentView frame]];
}
- (void) exitFullScreen:(NSView*)newView
{
// Restore old views
[_newViewContainer setFrame:[oldViewContainer frame]];
[newView setFrame:[oldView frame]];
}
The enter fullscreen method works exactly as expected, but for some reason I can't set the 2 Views back to their original size/location.
I don't know if it's because I can't just store the whole view, or if the origin is causing problems?
I've tried every combination, if somebody could help I'd be really grateful.
Thanks in advance.
You don't want to set oldView to newView, that makes them the same view, so when you resize newView to full screen, oldView's frame also is set to full screen. You want to save the old frame, not the view, so:
oldFrame = newView.frame; (to store the original frame)
then when you resize smaller,
[newView setFrame:oldFrame];

My UITabBar doesn't show up

Okay guys, maybe you can help me out with this one. I'm about ready to pull my hair out.
Recently I decided to upgrade my app and make it look better, and with that I wanted to move it into full support for iPad platforms as well. For a while everything worked great. Just press copy MainWindow.xib for iPad, add the views that I used on the iPhone configurations, and everything should be great, but that didn't work too well. Take a look:
Here is the iPhone screenshot:
Here is the iPad screenshot:
Where's the tab bar? I don't understand! I added the initial view when I was first putting it together, but when I linked all of the IBOutlets to the proper pieces, the tab bar no longer shows up.
Screenshot of IB:
Tab Bar properties:
Tint: A bluish color
Image Tint: A goldish color
Mode: Scale to fill
Tag: 0
User Interaction Enabled: (Checked)
Multiple Touch: (Unchecked)
Alpha: 1
Opaque: (Checked)
Hidden (Unchecked)
Clears Graphic Context: (Checked)
Clip Subviews: (Unchecked)
Autoresize Subviews: (Checked)
Stretching: (x,y,w,h):(0,0,1,1)
The viewController.h file is a delegate for UITabBar, UITextField, and UITextView
ViewDidLoad (bar is the IBOutlet for the tab bar):
- (void)viewDidLoad
{
[super viewDidLoad];
[self playMovieIntro];
NSURL *url = [NSURL URLWithString:#"http://www.faithlifefellowship.us/Audio/Sermons/NewSermonBanner.png"];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [UIImage imageWithData:data];
if(!image == NULL)
{
NewSermonBanner.image = image;
}
series = [[Series alloc] init];
SeriesName = #"";
NSRange range = [[[UIDevice currentDevice] name] rangeOfString:#"iPad"];
int i = 0;
if(range.location != NSNotFound)
i = 1;
bar.selectedItem = hometab;
//set delegates
[bar setDelegate:self];
[personalName setDelegate:self];
[personalEmail setDelegate:self];
[content setDelegate:self];
[prContent setDelegate:self];
[prName setDelegate:self];
[prEmail setDelegate:self];
}
I'm stumped. If you have any ideas or need any more information, let me know.
Thanks!
I'm going to give you a few things I'm getting in order to fix this. It will be tons easier if you could upload the source code for me/us to download and be able to pinpoint the problem.
Sometimes (I can't remember exactly when) I've had my navigation bar not show up because it was missing a connection.
Make sure you are not hiding the tab bar anywhere in code, though it doesn't seem to be the case since it shows up on iPhone.
Otherwise I'm gonna take a guess and say it's something in the NIB. Here are some things you can try:
Check all your connections to outlets
Make sure your objects in the NIB are of the correct class
Verify that the tab bar's "hidden" property is not check in Interface Builder
Compare and verify all the structure of the NIB file between iPhone and iPad
These are just some ideas :) again if you can post the code it would be fantastic.
Let us know how it goes,
Felipe
I had this problem with a app that support both iPhone and iPad. Make sure 'is initial view controller' is checked for the UITabBarController when you examine the view controller using the object inspector. When you do this, xcode will display a 'inbound' arrow on the left side of the view controller if you're using storyboards.

iPad - xib ignoring orientation on view loads

iPad question:
We have views that we're initializing from .xibs, with each .xib containing both landscape and portrait layouts.
The first view we load respects the orientation of the device, but any subsequent views seem to have confusion over which orientation they should be using. Or, rather, they seem to ignore orientation altogether and go with whichever orientation the .xib file was saved with. (i.e. We saved a .xib file in landscape, and even though the device was held in portrait position, it loaded the view from the .xib with a landscape layout.)
This is how we're calling in these views:
Settings *settingsTEMP = [[Settings alloc] init];
self.settings = settingsTEMP;
[self.view insertSubview:settings.view atIndex:0];
[settingsTEMP release];
The first of the calls (the one that respects the device's orientation) is in the viewDidLoad. The second, which uses a different view (the one that doesn't respect the device's orientation) is in a method that's called after a button is pressed, but uses the same syntax.
If we put both into viewDidLoad, they both respect the orientation.
We've tried searching for anyone having similar issues but have been thus far unsuccessful. Thanks in advance for any help you can provide.
I use the following
UIInterfaceOrientation toInterfaceOrientation = self.interfaceOrientation;
if ((toInterfaceOrientation == UIDeviceOrientationLandscapeLeft) ||
(toInterfaceOrientation == UIDeviceOrientationLandscapeRight))
{
NSLog(#"vSettings UIDeviceOrientationLandscape");
CGRect contentRect = CGRectMake(0,0,1024,768);
vSettings.bounds = contentRect;
CGRect myFrame = vSettings.frame;
myFrame.origin.x = 0.0;
myFrame.origin.y = 0.0;
vSettings.frame = myFrame;
UIImage *image;
image = [UIImage imageNamed: #"Default-Landscape.png"];
[backGroundSettings setImage:image];
}
I hate having to do this as it seems a right fudge but it works for me. Called just after I create the view.