Adding a view as subview to my app's window but it is not displayed - objective-c

I'm adding a view controller's view to my window in application:didFinishLaunchingWithOptions: but the view does not become visible. I'm not sure what's wrong.
Here's my app delegate code:
#class ToolBar;
#class MainViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
UIWindow *Window;
//UIToolbar *toolbar;
}
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) MainViewController *mainViewController;
#property (strong, nonatomic) ToolBar *toolbar;
#end
#import "AppDelegate.h"
#import "MainViewController.h"
#import "ToolBar.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize mainViewController = _mainViewController;
#synthesize toolbar =toolbar;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease];
self.mainViewController = [[[MainViewController alloc]init]autorelease];
[self.window addSubview:self.mainViewController.view];
//[self.window makeKeyAndVisible];
self.toolbar = [[[ToolBar alloc]init]autorelease];
[self.window addSubview:toolbar.view];
[self.window makeKeyAndVisible];
return YES;
}

Well, to start with you're not setting the frame for your toolbar.
But why are you adding a toolbar to your app's main window? I would expect it to be a subview of your mainViewController's view.

Related

Not Properly Passing Data Between Views

I'm trying to send text generated by the user from the AuthenticationViewController to MainProfileViewController. I'm not receiving an error report. The label just doesn't even appear in the MainProfileViewController. The outlets are correctly hooked up. Thanks for the help!
#import <UIKit/UIKit.h>
#class MainProfileViewController;
#interface AuthenticationViewController : UIViewController
{
MainProfileViewController *mainProfileViewController;
}
#property (retain, nonatomic) IBOutlet UITextField *usernameTextField;
#end
#import "AuthenticationViewController.h"
#import "MainProfileViewController.h"
#interface AuthenticationViewController ()
#end
#implementation AuthenticationViewController
#synthesize usernameTextField;
- (IBAction)createAccount: (id)sender {
{
mainProfileViewController = [[MainProfileViewController alloc] init];
mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mainProfileViewController.userText.text = usernameTextField.text;
[self presentViewController:mainProfileViewController animated:YES completion:nil];
}
}
#end
#import <UIKit/UIKit.h>
#import "AuthenticationViewController.h"
#interface MainProfileViewController : UIViewController
{
UITextField *userText;
}
#property (retain, nonatomic) IBOutlet UILabel *resultLabel;
#property (retain, nonatomic) IBOutlet UITextField *userText;
#property (retain, nonatomic) NSString *textPass;
#end
#import "MainProfileViewController.h"
#import "AuthenticationViewController.h"
#interface MainProfileViewController ()
#end
#implementation MainProfileViewController
#synthesize resultLabel, userText, textPass;
- (void)viewDidLoad
{
userText.text = textPass;
[super viewDidLoad];
}
#end
OK, there's a few things you're doing wrong.
First off, you're instantiating a local MainProfileViewController instead of instantiating the one that you have an ivar pointing to.
The second thing that you're doing wrong is trying to send over the view from the AuthenticationViewController to the MainProfileViewController. You shouldn't do that. Instead, pass the text itself; otherwise you are just overwriting pointers, and nothing will show up.
- (IBAction)createAccount: (id)sender {
// DON'T CREATE A LOCAL VARIABLE
// MainProfileViewController *controller = [[MainProfileViewController alloc] initWithNibName:#"MainProfileViewController" bundle:nil];
mainProfileViewController = [[MainProfileViewController alloc] initWithNibName:#"MainProfileViewController" bundle:nil];
mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// DON'T TRY SENDING THE VIEW OVER
// mainProfileViewController.userText = usernameTextField;
mainProfileViewController.userText.text = usernameTextField.text;
[self presentViewController:mainProfileViewController animated:YES completion:nil];
}
edit: I suppose it's not required that you have an ivar to the new view controller you want to present... But the point is that you cannot instantiate one, and then set the parameter on the one you did not instantiate (it will be nil).
Check your story board and see if you connected the Outlets properly.
There should be an IBOutlet object in your view controller that you should connect in story board.
Also check if you connected the correct view controller class to the view controller in storyboard
#import <UIKit/UIKit.h>
#class MainProfileViewController;
#interface AuthenticationViewController : UIViewController
{
MainProfileViewController *mainProfileViewController;
}
#property (retain, nonatomic) IBOutlet UITextField *usernameTextField;
#end
#import "AuthenticationViewController.h"
#import "MainProfileViewController.h"
#interface AuthenticationViewController ()
#end
#implementation AuthenticationViewController
#synthesize usernameTextField;
- (IBAction)createAccount: (id)sender {
{
MainProfileViewController *controller = [[MainProfileViewController alloc] initWithNibName:#"MainProfileViewController" bundle:nil];
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mainProfileViewController.textpass = usernameTextField.text;
[self presentViewController:controller animated:YES completion:nil];
}
}
#end
#import <UIKit/UIKit.h>
#import "AuthenticationViewController.h"
#interface MainProfileViewController : UIViewController
{
UITextField *userText;
}
#property (retain, nonatomic) IBOutlet UILabel *resultLabel;
#property (retain, nonatomic) IBOutlet UITextField *userText;
#property (retain, nonatomic) NSString *textpass;
#end
#import "MainProfileViewController.h"
#import "AuthenticationViewController.h"
#interface MainProfileViewController ()
#end
#implementation MainProfileViewController
#synthesize resultLabel, userText;
- (void)viewDidLoad {
usertext.text=textpass;
}
#end
You need to change your code on this part...
- (IBAction)createAccount: (id)sender
{
mainProfileViewController = [[MainProfileViewController alloc] init];
mainProfileViewController.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
mainProfileViewController.userText.text = usernameTextField.text;
[self presentViewController:mainProfileViewController animated:YES completion:nil];
}
Happy Coding...

Use of undeclared identifier in AppDelegate?

In my AppDelegate implementation file i get an error for an undeclared identifier but it appears to be declared, what am i overlooking?
#import "AppDelegate.h"
#import "ViewController.h"
#implementation AppDelegate
#synthesize window = _window;
#synthesize managedObjectContext=__managedObjectContext;
#synthesize managedObjectModel=__managedObjectModel;
#synthesize persistentStoreCoordinator=__persistentStoreCoordinator;
- (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;
[self.window makeKeyAndVisible];
return YES;
- (NSManagedObjectContext *)managedObjectContext
the error occurs at this line "Use of undeclared identifier "managedObjectContext"
In my AppDelegate header file:
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
#class ViewController;
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong, nonatomic) ViewController *viewController;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (nonatomic, strong) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, strong) NSPersistentStoreCoordinator *persistentStoreCoordinator;
- (NSURL *)applicationDocumentsDirectory;
- (void)saveContext;
#end

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:

Insert View controller before SplitViewController

I want to insert a RootViewController before my SplitViewController.
My code in AppDelegate is:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[_window addSubview:[_viewController view]];
[_window makeKeyAndVisible];
return YES;
}
Delegate call to my rootviewcontroller and in this i want to call to my splitviewcontroller:
- (void)viewDidLoad
{
[super viewDidLoad];
SplitViewController *splitViewController = [SplitViewController initApp];
[[self navigationController] pushViewController:splitViewController animated:YES];
}
In my SplitViewController i have this method:
+ (YetSplitViewController*) initApp
{
YetSplitViewController * cont = [[YetSplitViewController alloc] initWithNibName:#"YetSplitViewController" bundle:nil];
if (NO)
{
cont.splitWidth = 15.0;
}
return [cont autorelease];
}
After that not found good.......
My first code call from delegate to my SplitViewController:
#interface TemplateAppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) IBOutlet UIWindow *window;
#property (readonly, nonatomic) IBOutlet YetSplitViewController *splitViewController;
#property (readonly, nonatomic) IBOutlet MasterViewController *rootViewController;
#property (readonly, nonatomic) IBOutlet DetailViewController *detailViewController;
#end
#implementation TemplateAppDelegate
#synthesize window = _window;
#synthesize splitViewController = _splitViewController;
#synthesize rootViewController = _rootViewController;
#synthesize detailViewController = _detailViewController;
- (void)dealloc
{
[_window release];
[_splitViewController release];
[_rootViewController release];
[_detailViewController release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
if (NO)
{
_splitViewController.splitWidth = 15.0;
}
[_window addSubview:_splitViewController.view];
[_window makeKeyAndVisible];
return YES;
}
#end

Forcing a change in orientation when switching between tabs. (objective c)

so i'm testing this so i can use it on my bigger project.
I have A tabbarcontroller named TabBar
this TabBar has 2 tabs every tab has a navigationcontroller. A viewController with a button (OkButtonViewController) when you click this button you go to the viewcontroller with the label (LabelViewController). The OkButton View Controller is always in portrait and the labelViewController can switch orientation. this works only in one situation it goes wrong. when you are in the LabelViewController, orientated in landscape, and you switch tabs the OkButtonViewController is also in landscape, and stays in landscape. How can i force the viewcontroll to go back to portrait?
here is my code.
I probably need to add something in the TabBar or in the RotatingTabBarAppDelegate. I just don't know what.
TabBar.m
#import "TabBar.h"
#implementation TabBar
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return [self.selectedViewController shouldAutorotateToInterfaceOrientation:interfaceOrientation];
}
#end
RotatingTabBarAppDelegate.h
#import <Foundation/Foundation.h>
#import "TabBar.h"
#class RotatingTabBarAppViewController;
#interface RotatingTabBarAppDelegate : NSObject<UIApplicationDelegate>
{
IBOutlet UIWindow *window;
}
#property (nonatomic, strong) UIWindow *window;
#end
RotatingTabBarAppDelegate.m
#implementation RotatingTabBarAppDelegate
#synthesize window;
-(void) applicationDidFinishLaunching:(UIApplication *)application
{
UIViewController *tab1 = [[UIViewController alloc] init];
tab1.tabBarItem =[[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemTopRated tag:0];
UIViewController *tab2 = [[UIViewController alloc] init];
tab2.tabBarItem = [[UITabBarItem alloc] initWithTabBarSystemItem:UITabBarSystemItemSearch tag:1];
TabBar *tbc = [[TabBar alloc] init];
[tbc setViewControllers:[NSArray arrayWithObjects:tab1, tab2, nil]];
[window addSubview:tbc.view];
[window makeKeyAndVisible];
}
#end
OkButtonViewController.h
#import <UIKit/UIKit.h>
#interface OkButtonViewContoller : UIViewController
- (IBAction)ok;
#end
OkButtonViewController.m
#import "OkButtonViewController.h"
#import "LabelViewController.h"
#define kDetailSegue #"Detail"
#implementation OkButtonViewContoller
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)ok
{
[self performSegueWithIdentifier:kDetailSegue sender:#"test"];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:kDetailSegue]) {
((LabelViewController *)segue.destinationViewController).testTekst = sender;
}
}
#end
LabelViewController.h
#import <UIKit/UIKit.h>
#interface LabelViewController : UIViewController
#property (nonatomic, strong) NSString *testTekst;
#property (nonatomic, strong) IBOutlet UILabel *testLabel;
#end
LabelViewController.h
#import "LabelViewController.h"
#implementation LabelViewController
#synthesize testTekst;
#synthesize testLabel;
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
self.testLabel.text = testTekst;
}
#end
sorry for the bad english.
You cannot force a view to rotate, probably Apple would reject your app, you need to find another way to design this.