iPhone Adding Views on Windows Practice - iphone

I'm learning how Objective-C on the iPhone layers views onto the main window.
I've tried remaking a basic program to draw two different colored rectangular views onto the main UIView for an app, but it does not seem to work.
My current output is a white screen. However, when I close the application, it briefly shows the two rectangles as they're closing, making me believe they're drawn but not displayed for some reason.
Assuming the psuedo-code below (shortened from the original documentation for the sake of StackOverflow, but copied directly into my project) is fine, what else might I have to do to my project to get these rectangles to display?
Code from Apple in my TestAppDelegate.m file:
- (void)applicationDidFinishLaunching:(UIApplication *)application {
// Create the window object and assign it to the
// window instance variable of the application delegate.
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
window.backgroundColor = [UIColor whiteColor];
// Create and initialize a simple red square
// Create and initializesimple blue square
// Add the square views to the window
[window addSubview:redView];
[window addSubview:blueView];
// Once added to the window, release the views to avoid the
// extra retain count on each of them.
[redView release];
[blueView release];
// Show the window.
[window makeKeyAndVisible];
}
Link to Original in Apple Docs: http://tinyurl.com/y8alvgt

You may need to call setNeedsDisplay on the window
[window setNeedsDisplay];
this has the effect of telling CocoaTouch that your window contents have changed and need to be redrawn.
Here's a link to the documentation.

Related

UIWindow's makeKeyAndVisible not playing nice with ARC

I'm recreating some kind of UIAlertView specific to my app, so I'm subclassing UIWindow to do it. The window gets added to [UIApplication sharedApplication].windows, but is never actually shown. I trimmed it down to this small piece of code:
UIWindow *testWindow = [[UIWindow alloc] initWithFrame:self.view.bounds];
testWindow.backgroundColor = [UIColor blueColor];
[testWindow makeKeyAndVisible];
When I log [UIApplication sharedApplication].windows, I see:
"<UIWindow: 0x83774f0; frame = (0 0; 320 480); opaque = NO; autoresize = RM+BM; layer = <UIWindowLayer: 0x8377660>>",
"<UIWindow: 0x8382630; frame = (0 0; 300 400); layer = <UIWindowLayer: 0xf573e60>>"
And yet that second window with a blue background color is nowehere to be seen.
UPDATE: this seems to be an issue only when ARC is enabled. I created 2 new "single view" projects, one with ARC enabled and the other with ARC disabled. Both are identical and I add the UIWindow code to viewDidAppear: of the main view controller. When I run the apps in the simulator, the blue window only shows up in the ARC-disabled project. It looks like ARC gets rid of my UIWindow too quickly and so it doesn't even have time to show up. Making it __strong didn't help. Still clueless...
It looks like ARC gets rid of my UIWindow too quickly and so it doesn't even have time to show up. Making it __strong didn't help.
Making what __strong? The variable you showed in your question appears to be a local variable, which only exists until the method returns. When the method returns, the variable goes away, so nothing owns the window, so it will then get deallocated.
Assign the window's pointer to a __strong instance variable or a strong property. Then you'll have a longer-lasting ownership keeping the window alive. Set the ivar or property to nil after you dismiss the window.
As a side note, are you sure you want this to be a subclass of UIWindow and not UIView? Even UIAlertView is a view and not a window. If it creates its own window, you may want to do that—have the view create its own window as an implementation detail.
The docs mention that you should use UIScreen to determine the bounds: [[UIScreen mainScreen] bounds]
I'm not sure it's required, but you're not adding a UIView to your window.
I noticed the opaque = NO - try [window setOpaque:NO]; or something similar.
I tested your code and got:
Application windows are expected to have a root view controller at the end of application launch
So basically, use [window setRootViewController:...];. Don't ask me why, this really doesn't seem needed to me.

How to start an iphone app so that it doesnt use any nib files?

I've been reading about pros/cons of programming with/without an interface builder and i want to try writing an app from scratch. however, even with a window based application it creates a xib file and i would like to remove this but not sure what to do after. just really need that jump start. Thanks!
Fundamentally you have to specify the appDelegate in UIApplicationMain() (in main.m), that is... from:
int retVal = UIApplicationMain(argc, argv, nil, nil);
to:
int retVal = UIApplicationMain(argc, argv, nil, #"MyAppDelegate");
then in MyAppDelegate's method application:didFinishLaunchingWithOptions: you have to manually create your UI:
// initialize application's window
_window = [[UIWindow alloc] initWithFrame:MAIN_FRAME];
// activate and display application's window
[_window makeKeyAndVisible];
...and so on
There are few ways, one of the simple ones:
Go to the project files navigator, look for
"Supporting Files -> XXX-info.plist",
then look for this:
Main nib file base name:
Remove this.
Hope this help
You can create any project and while you add new viewController just uncheck the checkbox which says "With XIB for UserInterface" (shown below with a red arrow).
This would allow you to create viewControllers without the XIB.
But then you will have to put all the controls programmatically for the viewController
Dont use the viewController which comes in by default and add newViewController with the method mentioned above.
Then start creating controls you want like UIButton, UILabel, etc using its allocation(alloc) and initialization(init) methods and set its frames.
Then you need to set any attributes as per your requirement and then just add it as a subview to your main view of the viewwController. So it would be something like say adding a textField dynamically would be:
UITextField *txtField = [[UITextField alloc] initWithFrame:CGRectMake(150,30,40,24)];
txtField.textColor = [UIColor blueColor];
[self.view addSubview:txtField];
Hope this helps you.
Hopefully following step will be useful.
Open ProjectNameInfo.plist file and then remove the property called Main nib file base name (toward the bottom of the Information Property List). You can remove a property by clicking to select it and then pressing the Delete key.
Under Other Sources, open main.m, and change the last argument of the call to UIApplicationMain from nil to the name of your application delegate class (for example, #"ProjectNameAppDelegate").
Do following changes in AppDelegate class didFinishLaunchingWithOptions: method
//Get Rects of screen
CGRect screenBounds = [[UIScreen mainScreen] bounds];
//Allocate Window
m_window = [[UIWindow alloc] initWithFrame: screenBounds];
//Allocate custom Views
m_view = [[MyView alloc] initWithFrame: screenBounds];
//Add View And make window visible
[m_window addSubview: m_view];
[m_window makeKeyAndVisible];
return YES;
-> I learned it from book iPhone 3D programming: Philip Rideout : O'Reilly publication.
You should find above in goole books and read some pages for further understanding because only above explanation may not be enough.
Moreover, After doing above steps you can make any number of view controllers and views without using xib...so refer to various programming guide documents provided by apple.
Good Luck
I've made some nib-less project templates for Xcode 4: MinimalisticXcodeTemplates (GitHub).

Troubles with iPhone UINavigationController (UINavigationBar in wrong place)

I'm in the process of making some adjustments to an app, including changing to a navigation-based interface. As part of that change I've written a view controller that contains a UINavigationController. The problem is, for some strange reason the UINavigationBar and UIToolbar managed by the UINavigationController are displaced 20px down from where they should be. I've managed to produce the following example that demonstrates the issue:
// MyAppDelegate.m
#implementation MyAppDelegate
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.frame = [[UIScreen mainScreen] applicationFrame];
[self.window makeKeyAndVisible];
TestController* tc = [TestController new];
[self.window addSubview:tc.view];
return YES;
}
#end
// TestController.m
#implementation TestController
- (void)loadView
{
self.view = [[UIView alloc] initWithFrame:CGRectZero];
UINavigationController* navController = [UINavigationController new];
navController.view.backgroundColor = [UIColor blueColor];
[navController setToolbarHidden:NO animated:NO];
[self.view addSubview:navController.view];
}
#end
This produces the following result on my machine:
As you can see, the controls are 20px down from where I'd expect them to be. I've tried just about everything I can think of (various combinations of wantsFullScreenLayout, autoresizesSubviews, etc) with no positive effect. This also has nothing to do with programatically messing with the statusbar (as seems to be the case in most other examples of this I have come across), since I do not at any point mess with the statusbar. This occurs with or without a root view controller in the navigation controller - if there is one, it's contents are shifted 20px down too (so they actually are in the right place relative to the navigation bar and toolbar).
Any help much appreciated!
EDIT: After a bit of investigation, it seems that removing the line self.window.frame = [[UIScreen mainScreen] applicationFrame]; seems to correct the positioning of the navigation bar and toolbar and content. That said, now some other views in the application are in the wrong place (up underneath the statusbar). My understanding is that line is generally recommended to ensure that the window is the correct size?
As mentioned in my edit, removing the line self.window.frame = [[UIScreen mainScreen] applicationFrame]; seems to have corrected 95% of my problems. I've managed to fudge an approach to fix the other 5% by using the same background colour for my window and the remaining views having issues, but I can't say I'm thrilled with this solution - I shouldn't have to do that.
I'll keep experimenting, and if I find a better result will certainly post an edit here.
UINavigationController does not play nicely with being used as a subview; as you've noticed, it will often leave room for the status bar even when it is not actually under the status bar. If you're not trying to write your own container view controller, you should rework your code to not be adding a view controller's view as a subview at all.
That said, I've had luck fixing it by setting wantsFullScreenLayout to NO on the UINavigationController, which will make it not leave space for the status bar. You would, of course, want to do this just after allocating it, before loadView gets triggered.

Shake detection with using IBuilder or Load View without IBuilder [duplicate]

This question already has answers here:
iPhone Shake event not properly working
(2 answers)
Closed 2 years ago.
I am writing some application (let's say game - 1 view no cocoa controls) which needs to detect shakes.
As beginner with IOS have started with default openGL template (new one). Application works.
I have decided to add shakes. "motionBegan" don't work on "EAGLEview", so I have created view controller. Touches worked but "motionBegan" still not worked. (same like "viewDidAppear")
I thought that somehow IBuilder file is overlapping it.
So I have decided to resign from my IBuilder file and move forward without.
What I have now is:
main.m
int retVal = UIApplicationMain(argc, argv, nil, #"SimplePianoAppDelegate");
SimplePianoAppDelegate.m
window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
[window setUserInteractionEnabled:YES];
[window setMultipleTouchEnabled:YES];
viewController = [[InputControler alloc] init];
glView=[[EAGLView alloc] initWithFrame:window.bounds];
glView.hidden=NO;
viewController.view=glView;
[window bringSubviewToFront:glView];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[glView setMultipleTouchEnabled:YES];
[glView performSelectorOnMainThread:#selector(mainAppLoop) withObject:nil waitUntilDone:NO];
With this code touches and motions worked but I don't see anything. (just white screen) :(
I am sure that some of the lines above are not necessary, but I am trying all possible options. What is strange is that no matter if I create my glView or not "loadView" in viewcontroller is not called.
Thank you for help in advance.
Mariusz
while I have connected view outlet in view with controller it started to work... with this:
[self becomeFirstResponder]

Why does an UIView in iOS/ appear too high up?

I'm relatively new to iOS programming. I have made a few basic apps before, and I'm getting back into it once again.
A problem I had a while back, and now is coming to haunt me is this.
When I create a new UIViewController subclass, myViewController (with xib) and add this code to get the add the view to the window, the contents always appear too high up, by the same width as the default/recommended left/right margin.
The code to add the view to the window is this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
MyViewController *aViewController = [[MyViewController alloc] initWithNibName:#"MyViewController" bundle:[NSBundle mainBundle]];
[self setMyViewController:aViewController];
[aViewController release];
UIView *controllersView = [myViewController view];
[window addSubview:controllersView];
[window makeKeyAndVisible];
return YES;
}
For example, if I change the background colour of the view, I get a white strip at the bottom of the page when running in the simulator.
Any ideas?
Thanks
I don't think the problem is in that code. Although I guess you've done this already, it is probably a good idea to double check the .xib file. It may have an offset set in its position properties.
Also, it may be caused by the status bar not being set correctly. If you want to hide it, you can add an entry (UIStatusBarHidden -> true) in the info.plist file to set it to be hidden.
Either way check the dimensions of the .xib are the expected ones. And bear in mind the size of the status bar; the dimensions of the .xib file are different depending on whether the status bar is shown or not.