presentModalViewController in landscape - xcode

I've searched this forum (and others) but haven't found a solution yet.
I have a app in landscape mode. Everythin fine, but when I change to another view (with a UITable) its in portrait mode and doesnt rotate. I have tried whole day but cant figure it out.
#property (nonatomic, strong) IBOutlet UIView *aView;
#property (nonatomic, strong) IBOutlet UIViewController *aViewcontroller;
-----
aViewcontroller = [[UIViewController alloc] initWithNibName:#"ViewController" bundle:nil];
aViewcontroller.view = aView;
-----
[self presentModalViewController:aViewcontroller animated:YES];
//got my view in portrait orientation.
My views in my .xib file are all set to Orientation:Landscape.
This should be simple, right?
Thanks beforehand!

By default UIViewController supports only Portrait orientation. In order to support other orientations you need create new custom view controller which supports the orientations you need.
Steps:
Create new custom view controller ViewControllerLandscape
Then import it to your mainn view controller
#import "ViewControllerLandscape.h"
Change your code:
aViewcontroller = [[ViewControllerLandscape alloc] initWithNibName:#"ViewController" bundle:nil];
aViewcontroller.view = aView;
[self presentModalViewController:aViewcontroller animated:YES];
in your ViewControllerLandscape add the method:
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if((interfaceOrientation == UIInterfaceOrientationLandscapeLeft) ||
(interfaceOrientation == UIInterfaceOrientationLandscapeRight)) {
return YES;
}
return NO;
}

Related

Why is this Cocoa layout ambiguous?

I want to use layout constraints and create my UI programmatically. Here is a simple program that I'm hoping you can help me understand. In Interface Builder, I simply took the defaults -- there is an NSWindow with its default contentView. Below is all the code, and a screenshot.
I create a single button, and place it in the content view. Then I try to use constraints to make it fill the window. As you can see, it claims the layout is ambiguous. But when I click that button to "Exercise Ambiguity", nothing changes. The docs say it should choose a different possible layout.
I also think the content view is tightly surrounding the button and not filling the window, but I don't know how to force that with constraints.
// In AppDelegate.h
#interface AppDelegate : NSObject <NSApplicationDelegate> {
NSButton *_button;
}
#property (assign) IBOutlet NSWindow *window;
#end
// In AppDelegate.m
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSView *contentView = (NSView*)_window.contentView;
_button = [[NSButton alloc] init];
_button.title = #"Test";
_button.translatesAutoresizingMaskIntoConstraints = NO;
contentView.translatesAutoresizingMaskIntoConstraints = NO;
[contentView addSubview:_button];
NSDictionary *viewsDict = NSDictionaryOfVariableBindings(_button, contentView);
NSMutableArray *constraints = [[NSMutableArray alloc] init];
[constraints addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat:#"|[_button]|" options:0 metrics:0 views:viewsDict]];
[constraints addObjectsFromArray: [NSLayoutConstraint constraintsWithVisualFormat:#"V:|[_button]|" options:0 metrics:0 views:viewsDict]];
[contentView addConstraints:constraints];
[_window visualizeConstraints:constraints];
printf("Is layout ambiguous? %d\n", contentView.hasAmbiguousLayout);
}
#end
What if you visualize constraints on a subsequent iteration of the run loop, for example with a timer or by clicking a button, after the layout engine has had a pass at it? It may just be ambiguous because the layout engine hasn’t solved the system yet.
Edit: I ran your code, and am seeing the same issue. I’m also stumped now.

How to change UIImageView with a button in a another view

I've search the web a day for it but I can't find it:
How Can I change a UIImageView with a button in a another view on Xcode.
I'm new to Xcode.
Add this to your header file in the ImageView ViewController, and connect it:
#property (nonatomic, retain) IBOutlet UIImageView *imageView;
Add to the other ViewController's .m file (don't forget to import your first file):
- (IBAction)btnPressed {
TheOtherViewController *vc = [[TheOtherViewController alloc] init];
vc.imageView.image = [UIImage imageNamed:#"MyImage"];
}

Custom UIControls with Storyboard

I'm having trouble figuring this one out. I have a custom UIControl class set up to hold a UIImage and UILabel, and my UITableViewCell class holds two of these UIControls (leftProduct, & rightProduct)
#interface FeaturedProductControl : UIControl
#property (strong, nonatomic) IBOutlet UIImageView *featuredProductPhoto;
#property (strong, nonatomic) IBOutlet UILabel *featuredProductDescription;
#property (strong, nonatomic) Product *featuredProduct;
- (id)initWithProduct:(Product *)product;
#end
#interface FeaturedTableCell : UITableViewCell
#property (strong, nonatomic) IBOutlet FeaturedProductControl *leftProduct;
#property (strong, nonatomic) IBOutlet FeaturedProductControl *rightProduct;
#end
The images and labels are being filled in using the init method during cellForRowAtIndexPath, and they're coming through just fine. I have a target action associated with the UIControls in the Storyboard, but the productClicked: method doesn't seem to be called. I've tried changing it out to add the target action programmatically, no luck.
However, if I add an alloc/init to the code, the productClicked: method triggers properly, but unfortunately the UILabel and UIPhoto now come up empty onscreen. Since the UIControls are designed in Storyboard, I think I'm not supposed to do the alloc calls myself, but the TableViewController doesn't seem to like that it's not being called. I've tried calling alloc within [[FeaturedTableCell alloc] init], but it had no effect.
Contents of cellForRowAtIndexPath:
cellIdentifier = #"Featured Row";
FeaturedTableCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[FeaturedTableCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
}
FeaturedRow *featuredRowData = [self.productIndexModel objectAtIndexPath:indexPath]; // This contains the necessary products to fill the Row
Product *leftProduct = [featuredRowData.skuList objectAtIndex:0];
cell.leftProduct = [[FeaturedProductControl alloc] initWithProduct:leftProduct]; // Actions trigger, but no data
// cell.leftProduct = [cell.leftProduct initWithProduct:leftProduct]; // Data is filled in, but no action
// [cell.leftProduct addTarget:self action:#selector(productClicked:) forControlEvents:UIControlEventTouchUpInside]; // the storyboard mapping works without this line of code
if (featuredRowData.skuList.count > 1)
{
Product *rightProduct = [featuredRowData.skuList objectAtIndex:1];
cell.rightProduct = [cell.rightProduct initWithProduct:rightProduct];
// cell.rightProduct = [[FeaturedProductControl alloc] initWithProduct:rightProduct]; // Yes, these two are reversed from the left side code above for testing
[cell.rightProduct addTarget:self action:#selector(productClicked:) forControlEvents:UIControlEventTouchUpInside];
cell.rightProduct.hidden = NO; // right side column is hidden in case the number of products is odd
}
else
{
cell.rightProduct.hidden = YES;
}
[cell setNeedsLayout];
return cell;
Any idea what I'm doing wrong? I'm trying to keep as much of the initialization and setup as possible inside the storyboard, so I'd prefer not to go back to writing the whole UIControl programmatically.
Thanks everyone!
Figured it out. In my initWithProduct method, I included
self = [super init];
I commented out this line and the Touch Up Inside events started firing normally.

Blank Screen Issue in Simulator

I'm working on a game for the iPad, and I have it start up with a menu screen. For a while, the menu screen would come up just fine in the simulator. I'm using the main view controller that xcode provides when starting up a view-based application. But, unfortunately, I accidentally cut off the connection between the UIView and the view controller in interface builder, and after reconnecting it, the screen comes up as blank now. It works fine when I simulate the screen in interface builder, but not when running in xcode. Here's the code for the view controller:
//
// FunctionMachineViewController.h
// FunctionMachine
//
// Created by Kameron Schadt on 5/24/11.
// Copyright 2011 Willamette University. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface FunctionMachineViewController : UIViewController {
IBOutlet UITextField* equation;
IBOutlet UISlider* startLevel;
IBOutlet UITextView* startLevelNumber;
}
- (IBAction) startOnePlayer:(id)sender;
- (IBAction) startTwoPlayer:(id)sender startingEquation:(NSString*)equationUsed;
- (IBAction) sliderValueChanged:(UISlider*)sender;
#property(nonatomic, retain) IBOutlet UISlider* startLevel;
#property(nonatomic, retain) IBOutlet UITextField* equation;
#property(nonatomic, retain) IBOutlet UITextView* startLevelNumber;
#end
//
// FunctionMachineViewController.m
// FunctionMachine
//
// Created by Kameron Schadt on 5/24/11.
// Copyright 2011 Willamette University. All rights reserved.
//
#import "FunctionMachineViewController.h"
#import "GameViewController.h"
#implementation FunctionMachineViewController
#synthesize equation, startLevel, startLevelNumber;
- (IBAction)sliderValueChanged:(UISlider*)sender {
[startLevelNumber setText:[NSString stringWithFormat:#" %.1f", [sender value]]];
}
-(IBAction)startOnePlayer:(id)sender
{
GameViewController* GameView = [[GameViewController alloc] initWithNibName:nil bundle:nil];
[GameView isOnePlayer:YES];
[self presentModalViewController:GameView animated:YES];
}
-(IBAction)startTwoPlayer:(id)sender startingEquation:(NSString*)equationUsed
{
GameViewController* GameView = [[GameViewController alloc] initWithNibName:nil bundle:nil];
[GameView isOnePlayer:NO];
[self presentModalViewController:GameView animated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation {
return YES;
}
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
I didn't really see any problem here, so I'm assuming it has something to do with me reconnecting the view controller to the view. I don't have an actual view file that I'm using, just the viewcontroller. Can anybody help?
Check the setting of "Main nib file base name" in [YourApp]-info.plist, in the "Supporting Files" folder – if you've changed the name of your root view controller, you may need to change the name here as well.
For some odd reason my Referencing outlet for the App Delegate was disconnected.
Try creating a referencing outlet from delegate to File's Owner using the connections inspector (farthest right menu) for your App Delegate.

How do I put a background image on a text area (IN THE IPAD)

How do I put a template'd background image on a text area?
Not sure what you mean with "template'd"... but....
In interface builder, add the imageview behind the textview, uncheck the "opaque" box for the textview.
You can set the contents property of the text views layer.
#import <QuartzCore/QuartzCore.h>
...
- (void) viewDidLoad {
[super viewDidLoad];
textView.layer.contents = (id)[UIImage imageNamed:#"myImage.png"].CGImage
}
should work
If you want to add the image programmaticaly I managed to do it this way:
First in my textviewViewController.h I added an outlet for the UITextView.
#import <UIKit/UIKit.h>
#interface textviewViewController : UIViewController {
UITextView *textView;
}
#property (nonatomic, retain) IBOutlet UITextView *textView;
#end
Next, in my .xib I added my UITextView and connected my outlet.
Lastly in the viewDidLoad of textviewViewController.m I add the image as a subview to my textview.
- (void)viewDidLoad {
[super viewDidLoad];
// Change the pathForResource to reflect the name and type of your image file
NSString *path = [[NSBundle mainBundle] pathForResource:#"d" ofType:#"jpg"];
UIImage *img = [UIImage imageWithContentsOfFile:path];
UIImageView *imgView = [[UIImageView alloc] initWithImage:img];
[self.textView insertSubview:imgView atIndex:0];
[imgView release];
}
I tried the insertSubview:atIndex: with both 0 and 1 with the same result, but I would stick with 0 idicating that it's behind the textview.

Resources