UIPageControl in UIViewController - objective-c

I have a workspace with MFSideMenu. One voice of this menu has 3 UIViewController I would scroll with UIpageControl. it's possible?
How can it work?
Thanks

Basic example:
In your app delegate
#import "MFSideMenu.h"
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Do other stuff
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
}
For more information:
https://github.com/mikefrederick/MFSideMenu

Related

Objective c: iOS 13+ programmatically setting initially root view controller

I am trying to set an initial root view controller programmatically using SceneDelegate in iOS 14 (no storyboard at all). I have followed https://medium.com/#sapa.tech/xcode-11-3-remove-storyboard-from-project-6c48c9d11123 to remove storyboard from my app, and have seen many answers to this question in swift in Why is manually setup root view controller showing black screen?, and Set root view controller iOS 13 and iOS 13: Swift - 'Set application root view controller programmatically' does not work. However, I am looking for tips on how to do it in objective C instead. So far, I have this in my scene delegate, where ViewController is the class I am trying to set as the root view controller:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
UIWindowScene * winscene = [[UIWindowScene alloc]initWithSession:session connectionOptions:connectionOptions];
[self.window setWindowScene:winscene];
ViewController * viewController = [[ViewController alloc]init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
And in my AppDelegate:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
return YES;
}
I have tried to translate the Swift answers I found to objective c but I still end up with a black screen on the simulator. The app builds fine with no error, but I just cannot get the ViewController to show up instead of a black screen. Does anyone know what I am missing?
You're not supposed to create a UIWindowScene yourself, but to use the one given as parameter:
- (void)scene:(UIScene *)scene willConnectToSession:(UISceneSession *)session options:(UISceneConnectionOptions *)connectionOptions {
self.window = [[UIWindow alloc] initWithWindowScene:(UIWindowScene *)scene];
ViewController * viewController = [[ViewController alloc]init];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}

How to implement didSelectViewController

I want to catch the event when someone switches between tabs. I have the following two function in my appdelegate file:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
UITabBarController * uitbc = [storyboard instantiateViewControllerWithIdentifier:#"tabbarcontroller"];
uitbc.delegate = self;
[self.window addSubview:uitbc.view];
return YES;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"switching");
}
But the NSLog(#"switching"); never fires. The xcode issues a warning for the line uitbc.delegate = self; saying "Passing appdelegate const__strong to parameter of incompatible type id".
What am I doing wrong? I'm just following the accepted answer found here, except i'm instantiating my tabbarcontroller form story board:
how to get the event that switch tab menu on iphone
Update
Based on skram's suggestion, I wrote this for my appdelegate but the NSLOG(Switching) still doesn't fire:
#interface johnAppDelegate : UIResponder <UITabBarControllerDelegate>
I also updated my didFinishLauchingWithOptions:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
tabBarController = self.window.rootViewController.tabBarController;
tabBarController.delegate = self;
[window addSubview:tabBarController.view];
}
Good thing is that nothing crashes. I also no longer et the warning about incompatible types. But still, didSelectViewController doesn't fire.
in my appdelegate.h file, I changed the line
#interface wscAppDelegate : UIResponder <UIApplicationDelegate>
to
#interface wscAppDelegate : UIResponder <UIApplicationDelegate,UITabBarControllerDelegate>
Then in my CustomTabBarController in the viewDidLoad function i added these lines:
wscAppDelegate *appDelegate = (wscAppDelegate *)[[UIApplication sharedApplication] delegate];
self.delegate = appDelegate;
Then in appdelegate.m file, I added this function
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"hooray this works");
}
I also struggled with this a lot but my UITabBarNavigationController is pretty far from AppDelegate to set it there, and the only way to actually make it work was to do it not in the viewDidLoad of the UITabBarController subclass itself, but in its:
-(void)viewDidLayoutSubviews {
[ super viewDidLayoutSubviews ];
self.delegate = self;
}
And that solved it. Alternatively you can do it in any of the tab bar controlled viewcontrollers, but that just feels wrong.
You need your AppDelegate to conform to UITabBarControllerDelegate protocol. See below..
#interface YourAppDelegate : UIResponder <UITabBarControllerDelegate>

Load a UIViewController as main controller

I just created an empty iOS project and added a UIViewController with a nib file. This should load my main view.
How do I tell my application to load a certain UIViewController as main view? Do I have to set it on the app delegate or somewhere in xcode?
In your app delegate have a variable for the controller....
MyViewController myController;
Then you want...
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
myController = [[MYViewController alloc] initWithNibName:#"MyViewController" bundle:nil];
[self.window addSubview:myController.view];
[self.window makeKeyAndVisible];
return YES;
}
You can also hook this up via Interface Builder, but the above is how you would do it in code.

Assigning a custom ViewController as the rootViewController of the window

Is this a good way to set a custom ViewController as the rootViewController of the window?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = [[[CustomViewController alloc]init] autorelease];
[self.window makeKeyAndVisible];
return YES;
}
In most of Apple's examples they first declare a viewController property, and then:
RootViewController* theRVC = [[RootViewController alloc] init];
self.viewController = theRVC;
[theRVC release];
[self.window addSubview:self.rvc.view];
[self.window makeKeyAndVisible];
What is the diference between these two approaches and which is recommended?
The rootViewController property of UIWindow was recently introduced with iOS4. This new method seems to be the recommended approach advocated by Apple. Either approach works but I would stick with the new way of setting the rootViewController property only if you are not targeting early versions of iOS.

Applications are expected to have a root view controller at the end of application launch wait_fences: failed to receive reply: 10004003

when i launch my application in iphone emulator, it goes without any problem. If i launch on device it will crash as still as i open it. But when i launch emulator it says that problem:
Applications are expected to have a root view controller at the end of application launch
wait_fences: failed to receive reply: 10004003
What can i do?
This is my application delegate:
.h :
#import <UIKit/UIKit.h>
#class TrovaChiaviViewController;
#interface TrovaChiaviAppDelegate : NSObject <UIApplicationDelegate>
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet TrovaChiaviViewController *viewController;
#end
and this is .m
#import "TrovaChiaviAppDelegate.h"
#import "TrovaChiaviViewController.h"
#implementation TrovaChiaviAppDelegate
#synthesize window = _window;
#synthesize viewController = _viewController;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application
{
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
[application release];
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
}
- (void)applicationWillTerminate:(UIApplication *)application
{
}
- (void)dealloc
{
[_window release];
[_viewController release];
[super dealloc];
}
#end
I don't see where you are setting your viewController property, add
self.viewController = [[[TrovaChiaviViewController alloc] initWithNibName:#"TrovaChiaviViewController" bundle:nil] autorelease];
before
self.window.rootViewController = self.viewController;
I just had this issue when I was changing the application from iPhone only to iPad. The error message "Applications are expected to have a root view controller at the end of application launch" was happening because in the application-info plist, there was an entry specifying a nib file for MainWindow when I was no longer using a nib.
Although this is an older thread, I thought I'd contribute. Sometimes I select a template when creating new projects. There were two cases when I created empty projects. In both of these cases, I encountered the same problem.
Refer to this link.
http://www.mumuen.com/2011/09/applications-are-expected-to-have-root.html
In my case, the key was that I had failed to allocate space for both my window and root controller.