Can't override a simple method - objective-c

I've created a new project with two ViewControllers and imported a class that pushes ViewController from right to left instead LTR but can't manage to use it. I can see that pushViewController inside UIRightToLeft.m is not being called and I don't understand why.
My main goal is to get that working with RTL aniamtion.
#import "UIRightToLeft.h"
#implementation UIRightToLeft
- (id)initWithRootViewController:(UIViewController *)rootViewController
{
self = [super initWithRootViewController:rootViewController];
if (!self)
return nil;
return self;
}
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
NSLog(#"pushViewController");
// Add the viewController and a fake controller without animation. Then pop the fake controller with animation.
UIViewController *fakeController = [[UIViewController alloc] init] ;
[super setViewControllers:[[self viewControllers] arrayByAddingObjectsFromArray:[NSArray arrayWithObjects:viewController, fakeController, nil]] animated:NO];
[super popViewControllerAnimated:animated];
}
- (void)popViewControllerAnimatedStep2:(UIViewController *)viewController
{
// Push the new top controller with animation
[super pushViewController:viewController animated:YES];
// Remove the view that should have been popped
NSMutableArray *arr = [NSMutableArray arrayWithArray:[self viewControllers]];
[arr removeObjectAtIndex:[[self viewControllers] count]-2];
[super setViewControllers:[NSArray arrayWithArray:arr] animated:NO];
}
- (UIViewController *)popViewControllerAnimated:(BOOL)animated
{
NSLog(#"popViewControllerAnimated");
if (animated)
{
// Save the controller that should be on top after this pop operation
UIViewController *newTopController = [[self viewControllers] objectAtIndex:[[self viewControllers] count]-2];
// Remove it from the stack. Leave the view that should be popped on top
NSMutableArray *arr = [NSMutableArray arrayWithArray:[self viewControllers]];
[arr removeObjectAtIndex:[[self viewControllers] count]-2];
[super setViewControllers:[NSArray arrayWithArray:arr] animated:NO];
// Schedule the next step
[self performSelector:#selector(popViewControllerAnimatedStep2:) withObject:newTopController afterDelay:0];
return [arr objectAtIndex:[arr count]-1];
}
return [super popViewControllerAnimated:NO];
}
#end
ViewController.m:
#import "ViewController.h"
#import "UIRightToLeft.h"
#import "SecondViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)pressMe:(UIButton *)sender {
SecondViewController *next = [[SecondViewController alloc]init];
[self.navigationController pushViewController:next animated:YES];
}
#end
ViewController.h:
#import <UIKit/UIKit.h>
#interface ViewController : UIViewController
- (IBAction)pressMe:(UIButton *)sender;
#end
(In my ViewController there's only one button draged to the second ViewController with push)

After looking at your error log I think you actually need to have your navigation controller subclass UIRightToLeft.
If you are using a Storyboard, select your navigation controller and set Custom Class to UIRightToLeft.

Related

addObserver:forKeyPath is crashing the app, KVO, objective c

I am doing the parallax effect by using category by doing :
add and UIView into the uitableView (via category
add addObserver:forKeyPath so that whenever tableview is moving, i will reframe the view above
Details are below
UIScrollView+Parallax.h
#import <UIKit/UIKit.h>
#class ParallaxView;
#interface UIScrollView (Parallax)
#property (strong, nonatomic) ParallaxView *parallaxView;
- (void) addParallaxViewWith:(UIView*)parallaxView;
- (void) removeKVO;
#end
#interface ParallaxView : UIView
#end
UIScrollView+Parallax.m
static char parallaxKey;
#implementation UIScrollView (Parallax)
#dynamic parallaxView;
#pragma mark - Add parallax view to scrollView
- (void) addParallaxViewWith:(ParallaxView*)pView {
if ( !self.parallaxView) {
[self addSubview:pView];
[self setParallaxView:pView];
}
}
#pragma mark - Set parallaxView + register parallaxView as an observer
- (void) setParallaxView:(ParallaxView *)parallaxView {
objc_setAssociatedObject(self, &parallaxKey, parallaxView, OBJC_ASSOCIATION_ASSIGN);
/* THESE LINE ARE CRASHING THE APP */
// [self addObserver:self.parallaxView
// forKeyPath:#"contentOffset"
// options:NSKeyValueObservingOptionNew
// context:nil];
}
#pragma mark - Get parallaxView
- (ParallaxView*) parallaxView {
return (objc_getAssociatedObject(self, &parallaxKey));
}
#pragma mark - Remove
- (void)removeKVO {
[self removeObserver:self.parallaxView forKeyPath:#"contentOffset"];
}
#end
#implementation ParallaxView
-(id)init
{
//load xib from main bundle and assign it to self
self = [[[NSBundle mainBundle]loadNibNamed:#"Parallex"
owner:self
options:nil] objectAtIndex:0];
return self;
}
-(id)initWithFrame:(CGRect)frame
{
self = [self init];
[self setFrame:frame];
return self;
}
................
#end
And I am adding parallax to the table by doing
ParallaxView *pView = [[ParallaxView alloc]initWithFrame:CGRectMake(0, 0, 320, 160)];
[self.tableView addParallaxViewWith:pView];
However, [self addObserver:forKeyPath:options:context:nil] keeps crashing the app without no clues at all. If I comments this line out and app is not crashing but parallex effect is not working.
Any ideas for this problematics. Please help. Thanks
Problem in code
-(id)initWithFrame:(CGRect)frame
{
self = [self init];
[self setFrame:frame];
return self;
}
In above code self = [self init]; and [self setFrame:frame]; will go in recursion will give crash
,First fix this I guess it will solve your problem,It should be like this
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
and also loading View from nib using
self = [[[NSBundle mainBundle]loadNibNamed:#"Parallex"
owner:self
options:nil] objectAtIndex:0];
this code is really a bad idea.
you can refer THIS for this task.
Happy and clean coding...
#implementation ParallaxView
//Add Observe Method
-(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context{
if([keyPath isEqualToString:#"contentOffset"]){
NSLog(#"contentOffset:%#", [change objectForKey:NSKeyValueChangeNewKey]);
}
}
#end
Try to replace
objc_setAssociatedObject(self, &parallaxKey, parallaxView, OBJC_ASSOCIATION_ASSIGN);
with
//Change to OBJC_ASSOCIATION_RETAIN_NONATOMIC
objc_setAssociatedObject(self, &parallaxKey, parallaxView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
parallaxView should be a strong reference.

Custom delegate iOS 6 using ARC not working

I am a begginer with Objective-C programming.
I searched here and in other websites to a way to solve my problem but i did not find.
I want to create an login view before the system load. Then, after login i want to dismiss it.
On my project i use ARC and not use Storyboards.
When i debug and look into the function "efetuarLogin" the value of property delegate is 0x000000. I think it is blank, is not?
According to the tutorials and other tips i found on the internet, this is the right way to code delegate. Can you check this for me?
Here is the code:
Login.h
#import <UIKit/UIKit.h>
#class Login;
#protocol LoginDelegate <NSObject>
#required
- (void)loginDidFinish: (Login *)login;
#end
#interface Login : UIViewController{
__weak id<LoginDelegate> delegate;
}
#property (nonatomic, weak) id <LoginDelegate> delegate;
- (IBAction)efetuarLogin:(id)sender;
#end
Login.M
#import "Login.h"
#implementation Login
#synthesize delegate;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)efetuarLogin:(id)sender {
[delegate loginDidFinish:self];
}
#end
MainController.h
#import <UIKit/UIKit.h>
#import "Login.h"
#interface MainController : UITabBarController <LoginDelegate>
#end
MainController.m
#import "MainController.h"
#import "Login.h"
#import "ModalViews.h"
#import "Alerts.h"
#import "ViewPadrao.h"
#import "TableView.h"
#implementation MainController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
ModalViews *modalViews = [[ModalViews alloc] initWithNibName:#"ModalViews" bundle:nil];
Alerts *alerts = [[Alerts alloc] initWithNibName:#"Alerts" bundle:nil];
ViewPadrao *viewPadrao = [[ViewPadrao alloc] initWithNibName:#"ViewPadrao" bundle:nil];
TableView *tableView = [[TableView alloc] initWithNibName:#"TableView" bundle:nil];
self.viewControllers = #[modalViews, alerts, viewPadrao, tableView];
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
Login *login = [[Login alloc] initWithNibName:#"Login" bundle:nil];
[login setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:login animated:NO completion:nil];
}
- (void)loginDidFinish: (Login *)login{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Sua mensagem aqui" message:#"Mensagem" delegate:self cancelButtonTitle:#"Fechar" otherButtonTitles:nil];
[alert show];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
And the method didFinishLaunchingOptions of AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
mainController = [[MainController alloc] init];
window.rootViewController = mainController;
[window makeKeyAndVisible];
return YES;
}
Sorry for the high quantity of code. I am thankful since already!!
I am using the ViewDidAppear method to show Login to users. But when i dismiss and the MainController appears, the method viewdidapper creates the Login again.
How i do to show login once? When i tried to do this on viewdidload it did not work. The login did not appear.
In your MainViewController.m when you create login object
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
Login *login = [[Login alloc] initWithNibName:#"Login" bundle:nil];
login.delegate = self; //add this line
[login setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
[self presentViewController:login animated:NO completion:nil];
}
EDIT : After seeing OP's edit in the question (which should have been a different thread)
Your view Controller hierarchy is wrong. The ideal flow should be
1) Show Login controller directly from App delegate.
2) After successfull login, show MainViewController from Login Controller.

How to change between controllers when button tapped , iOS?

So i have my main viewController and all i have is a single button inside.
The code of the button is :
- (IBAction)eventsButton:(id)sender {
self.eventsViewController =[[EventsViewController alloc]initWithNibName:#"EventsViewController" bundle:nil];
[self.navigationController pushViewController:eventsViewController animated:YES];}
where EventsViewController is another controller where i want to navigate when this button is clicked. But when i click it nothing happens.. I dont navigate to the other controller.
ViewController.h
-------------------
#import <UIKit/UIKit.h>
#class EventsViewController;
#interface ViewController : UIViewController <UIActionSheetDelegate>
- (IBAction)eventsButton:(id)sender;
#property (strong, nonatomic) EventsViewController *eventsViewController;
#end
ViewController.m
-----------------
#import "ViewController.h"
#import "EventsViewController.h"
#implementation ViewController
#synthesize eventsViewController;
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (IBAction)eventsButton:(id)sender {
self.eventsViewController =[[EventsViewController alloc]initWithNibName:#"EventsViewController" bundle:nil];
[self.navigationController pushViewController:eventsViewController animated:YES];
}
#end
I put a breakpoint in the IBACTION and i see that the code is executed , but it never navigates me to the other controller. The other controller is just a simple controller i created with Xcode , it has all the code Xcode gives and nothing mine.
Any ideas?
My guess is you didn't put ViewController inside UINavigationController. First you have to alloc and init UINavigationController. So inside application: didFinishLounchingWithOptions:
ViewController *viewController = [[ViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:viewController]
[self.window setRootViewController:navigationController];
[self.window makeKeyAndVisible]
It should work.
[self.navigationController pushViewController:eventsViewController animated:YES];
its works only current view controller is navigation controller.Actually you implement this code in normal view controller not in navigation controller. Check with this following code replace above line
[self presentModalViewController:eventsViewController animated:YES];
So You need to implement navigation controller for your current view controller. In Appdelegate you change root view controller to navigation controller with your view controller.
UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:yourController] autorelease];
self.window.rootViewController = navController;

How to put a tab bar only in certain place in the app?

I want to put a tab bar for one place in my app. From there i can load some other view controllers, but that's it. I don't want it for my whole application. All examples I read through are making the tab bar controller as the rootviewcontroller of the window.
So how do i do that?
Thank you!
his may help you, Consider your view controller is HomeView where from you are going to push a tab bar controller, The HomeView is loaded in view at below:
#import "AppDelegate.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.
HomeView *homeview = [[HomeView alloc] initWithNibName:#"HomeView" bundle:nil];
UINavigationController *navigation = [[UINavigationController alloc] initWithRootViewController:homeview];
self.window.rootViewController = navigation;
[self.window makeKeyAndVisible];
return YES;
}
#end
then the .h, .m and .xib files of HomeView are going to be as follows:
In HomeView.h
#import <UIKit/UIKit.h>
#interface HomeView : UIViewController
{
IBOutlet UITabBarController *tabBarController;
}
-(IBAction)loadTabBar:(id)sender;
#end
and the HomeView.m is:
#implementation HomeView
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
-(IBAction)loadTabBar:(id)sender
{
[self.navigationController pushViewController:tabBarController animated:YES];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
and the .xib file must be as follows:
The tabBarController IBOutlet must be connected to the UITabBarController that on the .xib file. And that UITabBarController with two view controllers named FirstViewController, SecondViewController. Moreover that the HomeView must be inside a UINavigationController.
If not clarified with this answer, I'll update with detailed explanation.
The above is a way of loading tab bar controller using XIB method.
You can do this by coding like below by changing something in IBAction(button action) , In HomeView.m file:
#import "HomeView.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
#implementation HomeView
-(IBAction)loadTabBar:(id)sender
{
FirstViewController *firstView = [[FirstViewController alloc] initWithNibName:#"FirstViewController" bundle:nil];
SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
tabBarController = [[UITabBarController alloc] init];
tabBarController.viewControllers = [NSArray arrayWithObjects:firstView, secondView, nil];
[self.navigationController pushViewController:tabBarController animated:YES];
}
#end
You have to make tabBar in app delegate and then add when ever you want using this
-(void)addTabBarController
{
AppDelegate *appdelegte =(AppDelegate*)[[UIApplication sharedApplication]delegate];
//[[[appdelegte navigationController] view]removeFromSuperview];
[[appdelegte window]addSubview:[[appdelegte tabcontroller]view]];
[[appdelegte tabcontroller]setSelectedIndex:0];
}

Works on iPad Simulator but crashes on iPad

Hi I am trying to develop a new app on the ipad. I am using a spitTableView and adding a ModalPresentationPage to the view. This works perfectly on the xcode iPad sim but crashes on my iPad. just so you know I am using xcode 5BATA and running IOS 5 on my iPad.
here is my code
DetailViewController.h
#import <UIKit/UIKit.h>
#interface DetailViewController : UIViewController <UISplitViewControllerDelegate>{
}
-(IBAction)loadView:(id)sender;
#property (strong, nonatomic) id detailItem;
#property (strong, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
#property (strong, nonatomic) IBOutlet UIToolbar *toolbar;
#end
DetailViewController.m
#import "DetailViewController.h"
#import "ModalViewController.h"
#import "RootViewController.h"
#interface DetailViewController ()
#property (strong, nonatomic) UIPopoverController *popoverController;
- (void)configureView;
#end
#implementation DetailViewController
#synthesize detailItem = _detailItem;
#synthesize detailDescriptionLabel = _detailDescriptionLabel;
#synthesize toolbar = _toolbar;
#synthesize popoverController = _myPopoverController;
-(IBAction)loadView:(id)sender{
ModalViewController *mvc = [[ModalViewController alloc]initWithNibName:#"modalViewController"bundle:nil];
mvc.modalPresentationStyle = UIModalPresentationPageSheet;
mvc.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:mvc animated:YES];
}
- (id)initWithCoder:(NSCoder *)coder
{
self = [super initWithCoder:coder];
if (self) {
}
return self;
}
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
if (self.popoverController != nil) {
[self.popoverController dismissPopoverAnimated:YES];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
self.splitViewController.delegate = self;
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
}
- (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return YES;
}
#pragma mark - Split view
- (void)splitViewController:(UISplitViewController *)svc willHideViewController:(UIViewController *)aViewController withBarButtonItem:(UIBarButtonItem *)barButtonItem forPopoverController: (UIPopoverController *)pc
{
barButtonItem.title = #"Master";
NSMutableArray *items = [[self.toolbar items] mutableCopy];
[items insertObject:barButtonItem atIndex:0];
[self.toolbar setItems:items animated:YES];
self.popoverController = pc;
}
- (void)splitViewController:(UISplitViewController *)svc willShowViewController:(UIViewController *)aViewController invalidatingBarButtonItem:(UIBarButtonItem *)barButtonItem
{
// Called when the view is shown again in the split view, invalidating the button and popover controller.
NSMutableArray *items = [[self.toolbar items] mutableCopy];
[items removeObjectAtIndex:0];
[self.toolbar setItems:items animated:YES];
self.popoverController = nil;
}
#end
Parts not under NDA :-
You are leaking items in both the split view delegate methods.
Are you sure the XIB name is modalViewController? There could a problem of it being case sensitive on the device.