Setting the view programmatically - objective-c

I am trying to set my project without using storyboards in xcode and with objective c.
My appDelegate:
.h
#import <UIKit/UIKit.h>
#import "ViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#end
.m
#import "AppDelegate.h"
#interface AppDelegate ()
#end
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
return YES;
}
etc...
My viewController file:
.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
view.backgroundColor = [UIColor redColor];
[self.view addSubview:view];
}
I think my code is right and we should have a red screen when I run it, but I only get a black screen. Can someone tell me if I have forgotten something or is it something to do with the project settings. Thanks.

Add
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
to your application:didFinishLaunchingWithOptions:

You are missing two steps.
Initialize the window:
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
Make the window key & visible:
[self.window makeKeyAndVisible];
So all together, your code should look like this:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Initialize the window
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
// Add the view controller to the window
ViewController *vc = [[ViewController alloc] init];
self.window.rootViewController = vc;
// Make window key & visible
[self.window makeKeyAndVisible];
return YES;
}

Related

Is it possible to get a NSArray from AppDelegate?

Is it possible to get a NSArray from my AppDelegate? I need to send a array to other classes. And that array is generated in my AppDelegate. Thanks!
First of all you should have to alloc and init
In AppDelegate.h file
#property(nonatomic,retain)NSArray *books;
In AppDelegate.m file your array, then You can add objects in it.
_books = [[NSArray alloc]initWithObjects:#"test",#"test", nil];
You should have to create AppDelegate instance in your BooksDetailViewController like this,
in .h file
AppDelegate *appDelegate;
and in .m file
appDelegate = (AppDelegate*)[UIApplication sharedApplication].delegate;
Now you can access your array like this,
NSLog(#"Test Log :%#",appDelegate.books);
Output :
2016-07-14 13:04:08.211 Gridz[2490:39240] Test Log :(
test,
test
)
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
Now you can use any property or method like:
[appDelegate myProperty] or [appDelegate myMethod]
Hope this helps
It is possible to get the NSArray from AppDelegate to other classes.
AppDelegate.h
#import <UIKit/UIKit.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate> {
}
#property (nonatomic, strong) UIWindow *window;
#property (nonatomic, strong) ViewController *viewController; //For Xib
#property (nonatomic, strong) NSArray *arrayObjects;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize arrayObjects;
As I use Xib, I set the root view controller in below method.If you use stroy board, it is enough to add NSArray objects only.Don't need to set the root view controller.
//For XIB
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
arrayObjects = [[NSArray alloc]initWithObjects:#"Steve",#"jobs",#"Tim",#"Cook",nil];
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.viewController];
self.window.rootViewController = navController;
[navController setNavigationBarHidden:YES];
[self.window makeKeyAndVisible];
return YES;
}
//For Storyboard
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
arrayObjects = [[NSArray alloc]initWithObjects:#"Steve",#"jobs",#"Tim",#"Cook",nil];
return YES;
}
Then in ViewController.m
You can also import the AppDelegate in ViewController.m
#import "ViewController.h"
#import "AppDelegate.h"
#interface ViewController ()
#end
#implementation ViewController
Now in viewDidLoad method
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
AppDelegate *delegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
NSLog(#"The app delegate NSArray Objects are - %#",delegate.arrayObjects);
}
The NSLog results are
The app delegate NSArray Objects are - (
Steve,
jobs,
Tim,
Cook
)

Push to UIViewController from a UITabBarItem

I encounter a problem using UITabBarItem and UIButton in my application. My button is inside a UITabBarItem. When I press it I want to be pushed to another controller to display a PDF.
Here is a piece of code that works in other cases :
- (void)viewDidLoad {
UIImage* imageButton = [UIImage imageNamed:#"pdf-button.png"];
UIButton *buttonPDF = [UIButton buttonWithType:UIButtonTypeCustom];
buttonPDF.frame = CGRectMake(SCREEN_WIDTH/2 - 100, 100, 200, 36);
[buttonPDF setImage:imageButton forState:UIControlStateNormal];
buttonPDF.contentMode = UIViewContentModeScaleAspectFit;
buttonPDF.contentHorizontalAlignment = UIControlContentHorizontalAlignmentFill;
[buttonPDF addTarget:self action:#selector(displayPDFParams:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:buttonPDF];
}
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = documentParametres;
[self.navigationController pushViewController:pdfController animated:YES];
}
displayPDFParams is called but it not push me on my pdfController. I think it's because I'm not able to target the parent navigation controller of my application...
Anyone help would be much appreciated. Thanks in advance !
You need To initialize your root view controller with the navigation controller. Here is the code.
In your AppDelegate.h
#import <UIKit/UIKit.h>
#import "HomeViewController.h"
#class HomeViewController;
#interface IDSAppDelegate : UIResponder <UIApplicationDelegate>{
UINavigationController *nav;
}
#property (strong, nonatomic) HomeViewController *homeViewController;
#end
In your AppDelegate.m
#import "IDSAppDelegate.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.homeViewController = [[HomeViewController alloc] initWithNibName:#"HomeViewController" bundle:nil];
nav=[[UINavigationController alloc]initWithRootViewController:self.homeViewController];
self.window.rootViewController = nav;
[self.window makeKeyAndVisible];
return YES;
}
Problem solved by define a property in my UIViewController (as UITabBarItem) like this :
#property (nonatomic, retain) UINavigationController *superNavController;
And set it in my UITabBarController :
self.myViewController.superNavController = self.navigationController;
Finally I modified my displayPDFParams method :
-(void)displayPDFParams:(UIButton *)sender {
PDFProduitController *pdfController = [[PDFProduitController alloc] init];
pdfController.pdf = self.documentParametres;
[self.superNavController pushViewController:pdfController animated:YES];
}
Works perfectly !

receiver type 'AppDelegate' for instance message does not declare a method with selector ERROR

Can't seem to nail this issue here. Any idea what needs to be changed? I'm using Xcode 4.2.
Error: AppDelegate.m:23:6: error: receiver type 'AppDelegate' for instance message does not declare a method with selector 'setupHUD' [4]
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
LoginViewController *loginVC = [[LoginViewController alloc] init];
UINavigationController *navCon = [[UINavigationController alloc] initWithRootViewController:loginVC];
navCon.navigationBarHidden = YES;
self.window.rootViewController = navCon;
[self setupHUD];
[self.window makeKeyAndVisible];
return YES;
}
- (void) setupHUD
{
//setup progress hud
self.HUD = [[MBProgressHUD alloc] initWithFrame:self.window.bounds];
[self.window addSubview:self.HUD];
self.HUD.dimBackground = YES;
self.HUD.minSize = CGSizeMake(150.f, 150.f);
self.HUD.delegate = self;
self.HUD.isCancelButtonAdded = YES;
self.HUD.labelText = #"Loading";
}
Prior to Xcode 4.3, you need to forward declare methods called within the same #implementation block, so make sure to add the declaration of setupHUD to either the class extension (the list of declarations inside AppDelegate.m that starts with #interface AppDelegate ()), or the #interface block in AppDelegate.h.
For example, in AppDelegate.m:
#interface AppDelegate ()
// Other declarations...
- (void)setupHUD;
#end
Or in AppDelegate.h:
#interface AppDelegate : NSObject <UIApplicationDelegate>
// Other declarations
- (void)setupHUD;
#end

Who's know why black area is occured in iOS Simulator Retina 4-inch?

When I test in iOS Simulator Retina 3.5-inch, it is well work.
but when in Retina 4-inch, it is occured black area like following a picture.
I can't know this reason...
Would you give me a some idea?
Following is AppDelegate.h & m
#import <UIKit/UIKit.h>
#import "MenuViewCon.h"
#class TestViewCon;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) TestViewCon *viewController;
#end
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
self.viewController = [[[TestViewCon alloc] initWithNibName:#"ViewController_iPhone" bundle:nil] autorelease];
} else {
self.viewController = [[[TestViewCon alloc] initWithNibName:#"ViewController_iPad" bundle:nil] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
Next is TestViewCon.h & m
#import <Foundation/Foundation.h>
#interface TestViewCon : UIViewController
#end
#import "TestViewCon.h"
#implementation TestViewCon {
}
- (void)viewDidLoad {
UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(100, 100, 100, 100)] autorelease];
label.text = #"Retina 4 inch testing";
[label sizeToFit];
[self.view addSubview:label];
}
#end
This is because you app runs letterboxed.
It seems, that you don't support the iphone 5 within your project.
To do that, you need to add a Default-568h#2x.png Image to your project and set it as the launchimage for the iPhone 5.

iphone changing the initial scene

In my iphone application I have two scenes, both of them are view controllers.
Normally initial scene is set to the first one. When user plays it passes to the second one.
At this point when the user closes the app and restarts it, if the user has already played, it should start with the second scene, so the initial scene should change for that user.
How can I do this?
And i dont use a navigation controller.
If you are using xibs, you set this up in the app delegates
applicationDidFinishLaunchingWithOptions.
Create a global BOOL and check it's value on applications launch, E.g:
.h
#class ViewController;
#class SecondViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (strong, nonatomic) SecondViewController *SecondViewController;
.m
#import "ViewController.h"
#import "SecondViewController.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if (isSecondViewControllerBool == YES) {
self.SecondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
}else{
self.viewController = [[ViewController alloc] initWithNibName:#"ViewController" bundle:nil];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
For storyboards:
In the first view controllers viewDidLoad.
- (void)viewDidLoad
{
if (isSecondViewControllerBool == YES) {
UIViewController *secondVC = [self.storyboard instantiateViewControllerWithIdentifier:#"mySecondViewController"];
[self presentViewController:secondVC animated:NO completion:nil];
}else{
//present normally
}
}
If you are using storyboards and have decided to use my xib solution, delete the storyboard from the project, and remove it here as well: