Can't return firstResponder status - objective-c

Have set up my appDelegate to return a firstResponder status, but when I compile (it compiles fine), the console returns "Could not become the first responder" but doesn't tell why.
I have attached the code. Can you tell me what's wrong? Thanks in advance.
HypnosisterAppDelegate.m
#import "HypnosisterAppDelegate.h"
#import "HypnosisView.h"
#implementation HypnosisterAppDelegate
#synthesize window = _window;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
HypnosisView *view = [[HypnosisView alloc] initWithFrame:[[self window]bounds]];
[[self window]addSubview:view];
BOOL success = [view becomeFirstResponder];
if (success) {
NSLog(#"HypnosisView became the first responder");
} else {
NSLog(#"Could not become the first responder");
}
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
-(BOOL)canBecomeFirstResponder
{
return YES;
}

You've implemented canBecomeFirstResponder on HypnosisterAppDelegate, which is not a UIResponder sub-class. You then send becomeFirstResponder to your custom view (HypnosisView).
HypnosisView (not the HypnosisterAppDelegate) needs to do this:
-(BOOL)canBecomeFirstResponder
{
return YES;
}

Related

OS X how to designate startup viewcontroller

I am developing a simple browser project on mac, i didn't use the default viewcontroller.
Instead, I write a viewcontroller class BrowserViewController.
and write follow in appdelegate
#interface AppDelegate()
#property (nonatomic, strong) BrowserViewController *browserController;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
// Insert code here to initialize your application
self.browserController = [[BrowserViewController alloc] init];
[self.window makeKeyWindow];
[self.window setContentView:self.browserController.view];
}
#end
But when the app start up it lead to the default viewcontroller not the BrowserViewController. I really don't know the reason.
Thanks for the help, i have solve this problem. My solution is like this:
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
//set the frame of the window fit to the device's frame
//
self.window = [[NSWindow alloc] initWithContentRect:[[NSScreen mainScreen] frame] styleMask:NSBorderlessWindowMask backing:NSBackingStoreBuffered defer:NO ];
//
//
self.browserController = [[BrowserViewController alloc] init];
//set contentView
//
self.window.contentViewController = self.browserController;
//this is setting global backgroundColor of the window
//
self.window.backgroundColor = [NSColor whiteColor];
//this means the window is the window that will receive user interaction.
//
[self.window makeKeyAndOrderFront:self];
//[self.window makeKeyWindow];//NOTE: This is not working.
}
You can select Is Initial Controller in the storyboard.
Sounds like you're looking for (inside your didFinishLaunchingWithOptions method):
self.browserController = [[BrowserViewController alloc] init];
[self.window setRootViewController: self.browserController];
[self.window makeKeyAndVisible];
If you want it programmatically:
Note
This is for iOS, but the logic are identical. Hope this will be useful for some reason.. :)
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
//set the frame of the window fit to the device's frame
//
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
//what you have above is correct
//
self.browserController = [[BrowserViewController alloc] init];
//you can also directly set it like
//
BrowserViewController *mainView = [[BrowserViewController alloc] init];
//this is the most important, you need to set the window's rootViewController
//
self.window.rootViewController = mainView;
//this is setting global backgroundColor of the window
//
self.window.backgroundColor = [UIColor whiteColor];
//this means the window is the window that will receive user interaction.
//
[self.window makeKeyAndVisible];
return YES;
}
Hope this is helpful and informative.. Cheers! :)

how I use MFSideMenu?

I'm trying to create a MFSideMenu however I never used it and do not know what it missing. Can anyone write a short tutorial on how to finalize it?
-(void)viewDidLoad{
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
}
In AppDelegate.h
#import "MFSideMenuContainerViewController.h"
In AppDelegate.m
MFSideMenuContainerViewController *container = (MFSideMenuContainerViewController *)self.window.rootViewController;
UINavigationController *navigationController = [self.storyboard instantiateViewControllerWithIdentifier:#"navigationController"]; //I have instantiated using storyboard id.
UIViewController *leftSideMenuViewController = [self.storyboard instantiateViewControllerWithIdentifier:#"sideMenuViewController"]; //I have instantiated using storyboard id.
[container setLeftMenuViewController:leftSideMenuViewController];
[container setCenterViewController:navigationController];
In Your controller.m add:
- (IBAction)menuPressed:(id)sender
{
[self.menuContainerViewController toggleLeftSideMenuCompletion:nil];
}
- (MFSideMenuContainerViewController *)menuContainerViewController
{
return (MFSideMenuContainerViewController*)self.navigationController.parentViewController;
}
Put this code into your AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// init center, left, and right view controllers
MFSideMenuContainerViewController *container = [MFSideMenuContainerViewController
containerWithCenterViewController:centerViewController]
leftMenuViewController:leftMenuViewController
rightMenuViewController:rightMenuViewController];
self.window.rootViewController = container;
[self.window makeKeyAndVisible];
return YES;
}

RectView doesn't move away from UIWindow

I have the following simple code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
CGRect firstFrame = CGRectMake(300, 300, 150, 259); //CGRect is a struct not a simple object
ExampleView *view = [[ExampleView alloc] initWithFrame:firstFrame]; //crea la view
view.backgroundColor = [UIColor redColor]; //colora la view
[self.window addSubview:view];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
I'm working on XCode 5.1.1 but problem is that the red rect it is showed remains in the same position despite changing the values in CGRectMake. Do you have any idea why?
EDIT
This is the code
#import "ExampleView.h"
#implementation ExampleView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end

How to change the viewcontroller's background color from gray color to setting's color?

I'm trying to change the viewcontroller's color.
Someone let me know like following.
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
Here is AppDelegate.m
- (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 = [[[ViewController alloc] init] autorelease];
} else {
self.viewController = [[[ViewController alloc] init] autorelease];
}
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
This is ViewController.m
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
But, result is that viewcontroller is black screen.
How to change to settings backgroundcolor?
try this
self.view.backgroundColor=[UIColor clearColor];

BookController in iOS 5 Developer's Cookbook (Erica Sadun) does not start correctly in landscape mode

The problem I'm trying to solve:
I used chapter 05 example 06 BookController in chapter 5 (C05/06-Scrubbing Pages in https://github.com/erica/iOS-5-Cookbook.git ) in a project that have a UInavigatioController as rootViewController, but it does not start correctly in landscape mode. If you rotate the device portrait and return to landscape it begins to work.
To show the bug modify the example test bed delegate in main.c with the following:
#pragma mark Application Setup
#interface TestBedAppDelegate : NSObject <UIApplicationDelegate>
{
UIWindow *window;
}
#end
#implementation TestBedAppDelegate
TestBedViewController *tbvc;
UINavigationController *navigationController;
- (void) pushBookController: (UIGestureRecognizer *) recognizer
{
[navigationController pushViewController:tbvc animated:YES];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
tbvc = [[TestBedViewController alloc] init];
UIViewController *start = [tbvc controllerWithColor:[UIColor whiteColor] withName:#"white"];
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:#selector(pushBookController:)];
[start.view addGestureRecognizer:tap];
navigationController = [[UINavigationController alloc] initWithRootViewController:start];
window.rootViewController = navigationController;
[window makeKeyAndVisible];
return YES;
}
#end
My application starts landscape, so the problem is that the book controller does not recognize landscape mode at start (if you rotate after start it will work). Try it.
To recognize landscape at start, substitute in BookController the following method
// Entry point for external move request
- (void) moveToPage: (uint) requestedPage
{
//[self fetchControllersForPage:requestedPage orientation:(UIInterfaceOrientation)[UIDevice currentDevice].orientation];
[self fetchControllersForPage:requestedPage orientation:self.interfaceOrientation];
}
and add the following:
- (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation {return YES;}
Now the problem is that BookController does not appear at start, but again if you rotate it begins to work.
probably you solved this already, but try changing the Book's instantiation like this:
+ (id) bookWithDelegate: (id) theDelegate
{
NSDictionary *options = nil;
//Check the orientation
if (UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation])) {
//Setting Spine Location to middle for Landscape orientation
options = [NSDictionary dictionaryWithObject:
[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMid]
forKey: UIPageViewControllerOptionSpineLocationKey];
}
BookController *bc = [[BookController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:options];
bc.dataSource = bc;
bc.delegate = bc;
bc.bookDelegate = theDelegate;
return bc;
}
Note that I'm adding a statusBarOrientation check to set the spine.
Basically what you need is to check the orientation when you're creating the UIPageViewController, and I used the statusBarOrientation.
Oh, and don't forget to ensure you're getting the right orientation
- (BOOL) useSideBySide: (UIInterfaceOrientation) orientation
{
BOOL isLandscape = UIInterfaceOrientationIsLandscape(orientation);
// in case the UIInterfaceOrientation is 0.
if (!orientation) {
isLandscape = UIInterfaceOrientationIsLandscape([[UIApplication sharedApplication] statusBarOrientation]);
}
return isLandscape;
}
ps: The bookWithDelegate method corresponds to the BookController class within the example mentioned in the question.