Adding Label on Application Window from app delegate - objective-c

I am using Xcode 4.5 and iOS 6.0, I have selected the default single view template from the xcode, I just want to add the label on the application window but I am unable to do so. Please correct me.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = #"Test";
[label setBackgroundColor:[UIColor whiteColor]];
[self.window addSubview:label];
[self.window bringSubviewToFront:label];
[self.window makeKeyAndVisible];
return YES;
}
PS - I want my label on top of my ViewController view i.e. on the window so it will be always there despite changes in the views presented by window. I dont want to only show the label here.
I got the answer
[self.window.rootViewController.view addSubview:label];
Thanks all for giving the pointers.

Just Remove the RootviewController.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = #"Test";
[label setBackgroundColor:[UIColor whiteColor]];
[self.window addSubview:label];
[self.window bringSubviewToFront:label];
[self.window makeKeyAndVisible];
return YES;
}
if you dont want to only show the label here then use like below.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.Viewcontroller.view addSubview:label];

Add the label to self.window.rootViewController.view instead of self.window
UILabel *label =[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 20)];
label.text = #"Test"; [label setBackgroundColor:[UIColor whiteColor]];
[self.window.rootViewController.view addSubview:label];

Related

Opening new view on button click

I have a Navigation based Application named as diffView and in it I want to open new view on button click. For this a have created a view as file-new file-cocoa touch class-UIViewControllerSubclass named next1.
In diffViewAppDelegate.h I wrote as
IBOutlet UIButton *btn1;
#property (nonatomic,retain) UIButton *btn1;
-(IBAction)buttonPressed:(id)sender;
and in diffViewAppDelegate.m-
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
CGRect bounds=window.bounds;
UIView* view=[[UIView alloc]initWithFrame:bounds];
[view setBackgroundColor:[UIColor redColor]];
[window addSubview:view];
CGRect buttonFrame= CGRectMake(80, 240, 200, 30);
btn1=[[UIButton alloc] initWithFrame:buttonFrame];
[btn1 setTitle:#"space" forState:UIControlStateNormal];
[btn1.titleLabel setFont:[UIFont fontWithName:#"Verdana" size:20]];
[btn1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[view addSubview:btn1];
[self.window makeKeyAndVisible];
return YES;
}
-(IBAction)buttonPressed:(id)sender
{
next1 * nxt=[[next1 alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nxt animated:YES];
[nxt release];
}
I am not getting any error but when I click on the button nothing happens.
Please suggest me any solution.
Yes I have created the button in RootViewController but its not working.Even the button is not visible now. My code is like this-
CGRect buttonFrame= CGRectMake(80, 240, 200, 30);
btn1=[[UIButton alloc] initWithFrame:buttonFrame];
[btn1 setTitle:#"space" forState:UIControlStateNormal];
[btn1.titleLabel setFont:[UIFont fontWithName:#"Verdana" size:20]];
[btn1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchDown];
[self.view addSubview:btn1];
How i imagine it:
In your delegate you load - alloc/init the UINaviagationController and set its rootViewController property or just using initWithRootViewController: method when you init the NavigationController. Something like that.
viewController = [RootViewController alloc] init];
nvc = [[UINavigationController alloc] initWithRootViewController:viewController];
nvc.navigationBarHidden = YES;
[self.window setRootViewController:nvc];
[window makeKeyAndVisible];
In your RootViewController, from 'viewDidLoad:' method you create the button and add it to 'self.view' after that you implement the target selector and everything will be fine.
EDIT 1:
That is my application:didFinishLaunchingWithOptions: method in my AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
self.viewController = [[[ViewController alloc] init] autorelease];
self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
return YES;
}
That is my RootViewController
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
CGRect buttonFrame= CGRectMake(80, 240, 200, 30);
UIButton *btn1=[[UIButton alloc] initWithFrame:buttonFrame];
btn1.backgroundColor = [UIColor greenColor];
[btn1 setTitle:#"space" forState:UIControlStateNormal];
[btn1.titleLabel setFont:[UIFont fontWithName:#"Verdana" size:20]];
[btn1 addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn1];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)buttonPressed:(id)sender
{
NSLog(#"sender: %#", sender);
}
#end

Multi view App Programatically

I want to know if it is possible to make multi view app without using the Interface Builder.
I know how to make it with just using Xcode and Storyboards, but I just want to be able make it programmatically.
For example : If I have a UIViewController A (default view) with a UIButton and than I have another UIViewController B with UIWebView so when I click on the UIButton of UIViewController A I want to be able to display the second View (Without IB).
How can I set the action to the UIButton to display my second View (Without Interface Builder) ?
You can add a target to your button:
[button addTarget:self action:#selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
Then in ControllerA, add this method:
-(void)buttonPressed:(id)sender
{
[self.superview addSubview:controllerB.view];
[self.view removeFromSuperview];
}
On ViewControllerA
- (void)viewDidLoad
{
UIButton *btn=[UIButton buttonWithType:UIButtonTypeRoundedRect];
[btn setFrame:CGRectMake(50.0f,200.0f,60.0f,30.0f)];
[btn setTitle:#"Next" forState:UIControlStateNormal];
[btn setTitleColor:[UIColor blackColor] forState:UIControlStateHighlighted];
[btn addTarget:self action:#selector(btnPressed:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
-(IBAction)btnPressed:(id)sender
{
NextViewController *nxt=[[NextViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nxt animated:YES];
}
On viewControllerB
- (void)viewDidLoad
{
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(0,0, 320, 460)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
[self.view addSubview:webView];
}
Make sure that in AppDelegate.h should be navigation controller like this
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:self.self.viewController ];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}

How to deal with storyboard instead of xib file in such case?

I have an example where I have animation when the app starts. At the start an image is displayed, then moves from right to left, then disappears. Below is the code I have.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewwController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewwController;
[self.window makeKeyAndVisible];
//add the image to the forefront...
UIImageView *splashImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 480)];
[splashImage setImage: [UIImage imageNamed:#"Default"]];
[self.window addSubview:splashImage];
[self.window bringSubviewToFront:splashImage];
//set an anchor point on the image view so it opens from the left
splashImage.layer.anchorPoint = CGPointMake(0, 0.5);
//reset the image view frame
splashImage.frame = CGRectMake(0, 0, 320, 480);
//animate the open
[UIView animateWithDuration:1.0
delay:0.6
options:(UIViewAnimationCurveEaseOut)
animations:^{
splashImage.layer.transform = CATransform3DRotate(CATransform3DIdentity, -M_PI_2, 0, 1, 0);
} completion:^(BOOL finished){
//remove that imageview from the view
[splashImage removeFromSuperview];
}];
return YES;
}
What I want is to do this using a storyboard, so I changed the code as below.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
MyBookViewController *myNew = [[MyBookViewController alloc] init];
self.myBookViewController = myNew;
self.window.rootViewController = self.myBookViewController;
[self.window makeKeyAndVisible];
However I don't see my viewcontroller (MyBookViewController). I only see the image moving from right to left, then a black screen.
Any idea what is going wrong?
I just did it myself with help of this SO question.
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"MainStoryboard"
bundle: nil];
MyBook001ViewController *controller = (MyBook001ViewController*)[mainStoryboard
instantiateViewControllerWithIdentifier: #"firstStory"];
self.window.rootViewController = controller;
[self.window makeKeyAndVisible];
i think it is because of you are allocating new MyBookViewController. instead it you have to give reference of storyboard, on which you have your UI design for MyBookViewController, like this
MyBookViewController *myNew=[self.storyboard instantiateViewControllerWithIdentifier:#"MyBookViewController"];
be sure for ViewController identifier
UPdate1:
try with
MyBookViewController *myNew=[self.window.rootViewController];

Thread 1: EXC_BREAKPOINT (code=EXC_i386_BPT, subcode=0x0) error

I have an iPad app I am making, but it crashes on startup even though there are no errors or warnings, the output doesn't output anything besides "(lldb)", and it marks a pointer.
This is the image of the post build crash.
And here is the code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
/* UIImage *myImage = [UIImage imageNamed:#"de_dust2.png"];
myImageView = [[UIImageView alloc] initWithImage:myImage];
myScrollView.contentSize = CGSizeMake(myImageView.frame.size.width, myImageView.frame.size.height);
myScrollView.maximumZoomScale = 4.0;
myScrollView.minimumZoomScale = 0.75;
myScrollView.clipsToBounds = YES;
myScrollView.delegate = self;
// [myScrollView addSubview:myImageView];*/
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[self.viewController setRed:1];
[self.viewController setGreen:0];
[self.viewController setBlue:0];
[self.viewController setAlpha:1];
[self.viewController checkRotation];
return YES;
}
I now also noticed that the error was given at the [self.window makeKeyAndVisible]; line.
Your code looks ok, the crash could be caused by some exception thrown parsing the XIB file of the window ...
Take a look at the Window in Interface Builder and try to temporary remove all reference to any IBOutlet present in the View Controller ...

App crash with "Unable to restore previously selected frame" message

I can't figure out why that code leads to app crash.
AppDelegate.h
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.rootViewController = [[[RootViewController alloc]init]autorelease];
[self.window setRootViewController:self.rootViewController];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
Here is RootViewController.m code
-(void)loadView
{
UIView *view = [[UIView alloc]initWithFrame:CGRectMake(10, 10, 10, 10)];
[view setBackgroundColor:[UIColor lightGrayColor]];
[self.view addSubview:view];
[view release];
}
I get that message in the debugger
Unable to restore previously selected frame.
loadView is supposed to set the view. It is called when self.view is nil. Now you're calling [self.view addSubview:view]; UIKit calls loadView, and that creates an infinite recursion. You're supposed to do self.view = view; here.
loadView is responsible to set the view first. you missed to do that. Instead you added a view to self.view.
Change the code by below line:
self.view = view;
instead of [self.view addSubview:view];
Also it is advisable to call [super loadView] before returning from the function.