I saw my iOS app isn't allowed to be downloaded from the app store with the most recent upgrade to iOS 11, so I'm trying to update it. I have it in Xcode 9, and I've managed to now get the app started on my iPhone 6. However, I can't seem to get it to display after my initial screen display.
For example, the initial display successfully displays and shows a button called "start". That should pop up another display when you press it, but it doesn't (although it is reaching the code which displays it).
Here's the code, in a class named "StartController.m", which should be calling up the display:
NSLog(#"nav controller = %#", self.navigationController);
// push question controller onto the stack
[self.navigationController pushViewController:questionController animated:YES];
// the nav controller owns it now, we can release it
[questionController release];
Debugging shows this code is successfully called when the button is pressed, but after the release of questionController, nothing happens.
Here's the declaration of the questionController in StartController.h:
#interface StartController : UIViewController <ADBannerViewDelegate>{
QuestionController *questionController;
And here's the declaration of the same in StartController.m:
#implementation StartController
#synthesize questionController, settingsController, nextLevelViewController, appState;
The initial view (with the "start" button) is successfully displayed with this code in the "didFinishLaunchingWithOptions" method of the app delegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// create the start controller
self.startController = [[StartController alloc] init];
// transfer to it - not animated because it's the first view
[navController pushViewController:startController animated:NO];
// add the current nav controller view to the window view heirarchy
// [window addSubview:navController.view];
[self.window setRootViewController:startController];
// release becasause now the navController has it
[startController release];
// show the start controller
[self.window makeKeyAndVisible];
return YES;
}
For reference, here's the declaration of the navController in the app delegate header:
#interface QuizAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navController;
StartController *startController;
}
#property (readonly) BOOL iPad;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) StartController *startController;
#property (nonatomic, retain) IBOutlet UINavigationController *navController;
And here's the declaration in the implementation:
#implementation QuizAppDelegate
#synthesize window;
#synthesize startController;
#synthesize navController;
I tried not to post too much code for fear of obscuring the problem, but I'll add it if requested. Any ideas what the problem might be?
Related
I am looking to make an action from a button to another view coontroller.
When the button is pressed, the user is sent to another view controller.
However, when running the simulator and clicking the button, it is not going to the next view controller. (The next view controller is Dashboard).
Here is the .h file and .m file
`// ViewController.h
// Shopping List
//
// Created by Seenu on 2/8/16.
// Copyright © 2016. All rights reserved.
//
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *Login;
#property (weak, nonatomic) IBOutlet UITextField *emailTextfield;
#property (weak, nonatomic) IBOutlet UILabel *Password;
#property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
#property (weak, nonatomic) IBOutlet UIButton *button;
- (IBAction)SignIn:(id)sender;
#end`//
//
// ViewController.m
// Shopping List
//
// Created by Seenu on 2/8/16.
// Copyright © 2016. All rights reserved.
//
#import "ViewController.h"
#import "Dashboard.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize button;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)SignIn:(id)sender {
Dashboard *home = [[Dashboard alloc] initWithNibName:#"Dashboard" bundle:nil];
[self.navigationController pushViewController:home animated:YES];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Make sure your view controller have navigation controller. An easy way of doing this is create navigation controller in your applicationdidFinishLaunchingWithOptions in your AppDelegate Class. Set your first view controller as rootViewController of navigationController and add it to window. Go to your AppDelegate Class and add this code over there.
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIViewController *rootController =
[[RootViewController alloc]
initWithNibName:#"RootViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc]
initWithRootViewController:rootController];
self.window = [[UIWindow alloc]
initWithFrame:[[UIScreen mainScreen] bounds]];
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
In your storyboard, just right click (or hold down control button + click) at your button then drag to the view controller you want to navigate. When option menu showing, select "Show".
Also in your storyboard, please ensure that your view controller has embedded in a UINavigationController. (Select your first view controller, in Xcode menu click Editor > Embed In > Navigation Controller).
Sorry, I wrote this answer instantly, if you need some picture, I can attach it in the next update.
I'm fairly new iOS development. I have multiple viewControllers for my app, for example -
A login screen
A main / my profile screen
A leader board / standings screen
etc..
Each of these screens has its own viewController.
What's the best way to transition between any two arbitrary viewControllers with an animation?
My current approach is -
Keep a reference to each viewController in AppDelegate.m
Keep changing the window's root controller as needed
This is both cumbersome and seems pretty inefficient, plus I'm not quite sure how to incorprate animated transitions here.
I see some examples with UINavigationController, but it seems like that operates as a "stack" of views that you can go into and then back out of. I'm not looking to keep a history here, just switch from any view to another.
Any good ways to accomplish this?
Thanks!
Try this Code.
AppDelegate.h
#import <UIKit/UIKit.h>
#import "LoginViewController.h"
#interface AppDelegate : UIResponder <UIApplicationDelegate>
#property (strong, nonatomic) UIWindow *window;
#property (strong,nonatomic) UINavigationController *navigationController;
#property (strong,nonatomic) LoginViewController *loginVC;
#end
AppDelegate.m
#import "AppDelegate.h"
#import "LoginViewController.h"
#implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.loginVC = [[LoginViewController alloc]initWithNibName:nil bundle:nil];
self.loginVC.title = #"Login Page";
self.navigationController = [[UINavigationController alloc]initWithRootViewController:self.loginVC];
self.window.rootViewController = self.navigationController;
[self.window makeKeyAndVisible];
}
LoginViewController.h
#import <UIKit/UIKit.h>
#import "MyProfileViewController.h"
#interface LoginViewController : UIViewController
#property (strong,nonatomic)MyProfileViewController *myProfileVC;
#end
Login button action in LoginViewController.m file
- (IBAction)pushMyProfileView:(id)sender
{
self.myProfileVC = [[MyProfileViewController alloc]initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:self.myProfileVC animated:YES];
}
Basically you can achieve that by using:
Apple containers (UINavigationController, UITabbarController, UISplitController)
Containment API
Using "normal" view controller and modal presentation
Containment API is probably what you are looking for, you create a UIViewController that is responsible to manage the presentation of its child view controllers. Here is Apple Documentation, there are plenty of examples inside.
As a beginner I believe the easiest solution for you it to use a Storyboard and create a segue between your View Controller.
Inside your ViewController you should override prepareForSegue: method to pass data to the segue.destinationViewController
Using Xcode 4.2, my app runs in iOS 5.0 simulator. It runs on a 3G iPhone with iOS 4.2.1. It does not run on an iPod with iOS 3.1.3.
This is boilerplate code I got from any number of tutorials, but on the iOS 3.1.3 device, after displaying my Default.png, this line fails:
self.window.rootViewController = self.viewController;
with 'Unrecognized selector sent to instance' in my ykAppDelegate.m here:
#import "ykAppDelegate.h"
#import "ykViewController.h"
#implementation ykAppDelegate
#synthesize window;
#synthesize viewController;
#pragma mark -
#pragma mark Application lifecycle
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the view controller as the window's root view controller and display.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
With a more than cursory glance at the code, I notice viewController isn't apparently instantiated (except for #synthesize); it's just declared in my ykAppDelegate.h:
#import <UIKit/UIKit.h>
#class ykViewController;
#interface ykAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ykViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet ykViewController *viewController;
#end
Is there a small tweak I can make so this will work in iOS 3.1?
In pre-iOS5 operating systems UIWindow doesn't have a property named 'rootViewController'. An idiomatic solution is to simply add the view controller's view as a subview to your application's key window:
[window addSubview:self.myViewController.view];
This worked for me
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// Set the view controller as the window's root view controller and display.
[window addSubview:self.viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
After switching to Xcode 4, I thought I would be able to build and run my application exactly like I could in Xcode 3.
Turns out I can't.
Xcode 4 has a funny way of never showing the app's view controller, which is strange.
I can tell Apple's going to eventually force us to switch, which will result in my app being inoperable.
It gets to application:didFinishLaunchingWithOptions: without any errors, before hanging. Eventually the application crashes on the device - but stays on Default.png forever in the simulator.
I thought I could go and edit the application:didFinishLaunchingWithOptions: method the instantiate an instance of the view controller itself and add it to the window - only to reveal that didn't work either.
After numerous failed attempts at this - creating separate UIWindows for the main view controller - I decided the add it to a navigation controller.
Luck then struck me - but only in the simplest of forms. I looked in the log and see that applicationDidBecomeActive: had been called.
But, as usual, no such luck with any sort of view being displayed.
I then decided to see if I could add a UIView with a blue background color and a few UI elements (buttons, labels, etc...) to the window and see if that would work.
Funnily enough it did.
But why not for the main view controller? Not once in Xcode 4 have I successfully had it run my app (even opening it after it has been built fails). I've tried changing the compiler to the same as in Xcode 3, no luck.
I am honestly really confused as to why the application's view controller won't display.
For anyone who wants to give it an attempt as to why it isn't working that would be gratefully appreciated.
Here's the code for the AppDelegate, if you need the code for the view controller I can paste it here, however it's over 2000 lines.
Anyway, here's the .m file:
#import "DocumentationAppDelegate.h"
#import "DocumentationViewController.h"
#implementation DocumentationAppDelegate
#synthesize window;
#synthesize viewController;
#synthesize navigationController;
- (void)applicationWillEnterForeground:(UIApplication *)application {
NSLog(#"In method %#, which is in class %#.", NSStringFromSelector(_cmd), NSStringFromClass([self class]));
}
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(#"In method %#, which is in class %#.", NSStringFromSelector(_cmd), NSStringFromClass([self class]));
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSLog(#"In method %#, which is in class %#.", NSStringFromSelector(_cmd), NSStringFromClass([self class]));
DocumentationViewController *vc = [[DocumentationViewController alloc] init];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:vc];
controller.navigationBarHidden = YES;
UIWindow *win = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[win addSubview:controller.view];
[win makeKeyAndVisible];
return YES;
}
- (void)applicationWillTerminate:(UIApplication *)application {
}
- (void)dealloc {
[window release];
[super dealloc];
}
#end
and the .h
#import <UIKit/UIKit.h>
#class DocumentationViewController;
#interface DocumentationAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
DocumentationViewController *viewController;
UINavigationController *navigationController;
}
#property (nonatomic, retain) IBOutlet UINavigationController *navigationController;
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet DocumentationViewController *viewController;
#end
If any one could help me here it would be tremendously appreciated.
Your app delegate already has the properties window, viewController and navigationController. So you can have the application:didFinishLaunchingWithOptions: method like this,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
viewController = [[DocumentationViewController alloc] init];
navigationController = [[UINavigationController alloc] initWithRootViewController:vc];
navigationController.navigationBarHidden = YES;
[self.window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
I've been banging my head against this all day, it seems like something simple but I can't figure it out.
I've got an iOS app that I created using the "View-based Application" template in XCode. Here is essentially the code I have:
AppDelegate.h:
#import <UIKit/UIKit.h>
#interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
MainViewController *viewController;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet MainViewController *viewController;
#end
AppDelegate.m:
#import "AppDelegate.h"
#import "MainViewController.h"
#implementation AppDelegate
#synthesize window, viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[self.window addSubview:viewController.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
#end
MainViewController.h:
#import <UIKit/UIKit.h>
#interface MainViewController : UIViewController {
}
-(IBAction) button:(id)sender;
#end
MainViewController.m:
#import "MainViewController.h"
#import "ModalViewController.h"
#implementation MainViewController
...
-(IBAction) button:(id)sender {
ModalViewController *mvc = [[[ModalViewController alloc] initWithNibName:NSStringFromClass([ModalViewController class]) bundle:nil] autorelease];
[self presentModalViewController:mvc animated:YES];
}
#end
There's nothing of interest in the ModalViewController.
So the modal view should display when the button is pressed. When I press the button, it hangs for a second then crashes back to the home screen with no error message.
I am stumped, please show me what I'm doing wrong!
There's nothing of interest in the ModalViewController.
Although there may not be anything of interest, there could still be something causing the problem.
Does your View Controller override the function loadView?
A problem that has got me a few times is if you don't call [super loadView]; first in your overriden loadView method. Not doing this causes a crash when moving into that View Controller.
Good luck in trying to solve this one!
N