Tab bar element onclick IOS - ios

I have a master utility application embedded in a tab bar. Now, for this tab bar, i have the following.
#interface MainTabViewController : UITabBarController< UITabBarControllerDelegate>{
}
#end
#implementation MainTabViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
NSLog(#"One");
if(self.tabBarController.selectedIndex==1)
{
NSLog(#"clicked");
}
if (self.tabBarController.selectedIndex==2) {
NSLog(#"Helo");
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
The code is not reaching the didSelectViewController at all. I am new to IOS. Could anyone plz guide where i am going wrong??

Add delegate method to the UITabBarController
either using storyboard (if you are using)
or as it is UITabBarController do using
self.delegate = self ;

Related

How to pass value from subclass to super class during initialization

Suppose, I've two view controller. ViewController1 is the sub class of ViewController2. From subclass (ViewController1), I called superclass init method & passed a value by parameter & assigned that value to the superclass variable. But In superclass (ViewController2)'s viewDidLoad method, that value is always null. Why ? How can I pass that value from sub to super so that I can get that value in super class's viewDidLoad method ?
ViewController1.h
#interface ViewController : ViewController2
#end
ViewController1.m
#implementation ViewController
- (instancetype)init
{
self = [super initWithName:#"Hello"];
if (self) {
NSLog(#"1 :");
}
return self;
}
- (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.
}
#end
ViewController2.h
#interface ViewController2 : UIViewController
#property (nonatomic, strong) NSString *name;
-(instancetype)initWithName:(NSString *)name;
#end
ViewController2.m
- (instancetype)initWithName:(NSString *)name
{
self = [super init];
if (self) {
self.name = name;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
NSLog(#"2 : %#", self.name);
}
#end
I tired your code and it worked just fine. I suspect, that you don't instantiate your ViewController directly. Do you use storyboard in your project? If yes - you should override initWithCoder: in ViewController.
However, it is bad idea to set any properties of view controllers during init-family methods. According to Apple recommendations, all customisation should be done in -viewDidLoad or -viewWill/DidAppear methods.
If you absolutely in need to set name property from outside, it is better you assign it directly, not trough passing argument to init method.
One more thing - initWithNibName:bundle: is Designated initializer. It means, that your subclasses absolutely must in the end call it during initialisation chain.
Updated - 16-Mar-2016
Example :
ViewController1.h
#import <UIKit/UIKit.h>
#import "ViewController2.h"
#interface ViewController1 : ViewController2
#end
ViewController1.m
#import "ViewController1.h"
#interface ViewController1 ()
#end
#implementation ViewController1
-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
//self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil andUserName:#"Ramesh Annadurai"];
// we can use above init method or below method, both will work
self = [super initWithName:#"Test Name"];
if (self) {
[self.view setBackgroundColor:[UIColor brownColor]];
}
return self;
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
//self = [super initWithCoder:aDecoder];
// If you are using storyboard use this initWithCoder method.
self = [super initWithName:#"Test Name"];
if (self) {
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end
ViewController2.h
#import <UIKit/UIKit.h>
#interface ViewController2 : UIViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andUserName:(NSString *) userName;
- (instancetype)initWithName:(NSString *) userName;
#end
ViewController2.m
#import "ViewController2.h"
#interface ViewController2 ()
#property (strong, nonatomic) NSString *userName;
#end
#implementation ViewController2
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil andUserName:(NSString *) userName
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.userName = userName;
}
return self;
}
- (instancetype)initWithName:(NSString *) userName
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
self.userName = userName;
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(#"User Name : %#", self.userName);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
#end

Revert to previous controller after xx amount of seconds

I have a very simple app with very little code. In my ViewController I have done no code, I have only added a navigation bar which contains a next button with a modal to the VideoController. What I would like to achieve is after the next button is pushed in ViewController, allow the user to view VideoController for 5 seconds and then automatically return to the previous ViewController. Here is the code I have added in my VideoController.m
#import "VideoController.h"
#interface VideoController ()
#end
#implementation VideoController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//code added
[self performSelector:#selector(goBack) withObject:nil afterDelay:5.0];
}
//code added
-(void)goBack{
[self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
\
#end
I am not receiving any errors however this is not reverting me back to the previous page as desired. What am I missing?
try this code
- (void)viewDidLoad
{
[super viewDidLoad];
//code added
//[self performSelector:#selector(goBack) withObject:nil afterDelay:5.0];
[NSTimer scheduledTimerWithTimeInterval:3.0
target:self
selector:#selector(goBack)
userInfo:nil repeats:NO];
}
//code added
-(void)goBack{
[self.navigationController popViewControllerAnimated:YES];
}

Autolayout IOS 7 Storyboard Force Landscape or Portrait

I have a single storyboard where a majority of the views should be in portrait mode but one should be in Landscape. I've down a significant amount of searching but keep coming up a little blank.
My current implementation which isn't exactly seeming to work as I hoped consists of the following bits of code I've pieced together from this website:
I've made a RotationalViewController which extends from UINavigationController
#import "RotationControlledViewController.h"
#interface RotationControlledViewController ()
#end
#implementation RotationControlledViewController
- (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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)shouldAutorotate
{
return [[self.viewControllers lastObject] shouldAutorotate];
}
-(NSUInteger)supportedInterfaceOrientations
{
return [[self.viewControllers lastObject] supportedInterfaceOrientations];
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
UIInterfaceOrientation ret = [[self.viewControllers lastObject] preferredInterfaceOrientationForPresentation];
return ret;
}
#end
And then I've made a PortraitViewController
#import "PortraitViewController.h"
#interface PortraitViewController ()
#end
#implementation PortraitViewController
- (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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)shouldAutorotate
{
return YES;
}
- (NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationMaskPortrait | UIInterfaceOrientationMaskPortraitUpsideDown;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationPortrait;
}
and a LandscapeViewController
#import "LandscapeViewController.h"
#interface LandscapeViewController ()
#end
#implementation LandscapeViewController
- (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.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(BOOL)shouldAutorotate
{
return YES;
}
-(NSUInteger)supportedInterfaceOrientations
{
return UIInterfaceOrientationLandscapeRight | UIInterfaceOrientationLandscapeLeft;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation
{
return UIInterfaceOrientationLandscapeRight;
}
#end
I have played around with changing some of the settings but to no avail. My general plan of attack has been to make specific view controllers inherit from either LandscapeViewController or PortraitViewController if I want to lock them to a specific orientation.
Things seem to be working for portrait view ( i can lock controllers into portrait view) but I seem unable to load my 1 landscape view and have it a) auto rotate to landscape upon load, or b) rotate to landscape at all.
Any suggestions would be very appreciated.
Applying the solution presented in:
Force controllers to Change their orientation in Either Portrait or Landscape
I was able to make things work.
In both my PortraiteViewController and LandscapeViewControllers I modified the viewDidLoad method to:
PortraiteViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
LandscapeViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeLeft animated:YES];
UIViewController *mVC = [[UIViewController alloc] init];
[self presentModalViewController:mVC animated:NO];
[self dismissModalViewControllerAnimated:NO];
}
This solution MAY stop working after IOS7 because both presentModalViewController and dismissModalViewController are deprecated.

iOS: Add UIView below UINavigationBar

if I try to add a view to the navigation bar in my custom UINavigationController with this code:
- (void)viewDidLoad {
[super viewDidLoad];
self.sv = (MyCustomView *)[[[NSBundle mainBundle]loadNibNamed:#"MyCustomView" owner:self options:nil] objectAtIndex:0];
self.sv.frame = CGRectMake(self.navigationBar.frame.origin.x, self.navigationBar.frame.size.height, self.navigationBar.frame.size.width, heightOfMyCustomView);
[self.navigationBar addSubview:self.sv];
}
and
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated
{
[super pushViewController:viewController animated:animated];
viewController.view.frame = CGRectMake(viewController.view.frame.origin.x, viewController.view.frame.origin.y + heightOfMyCustomView, viewController.view.frame.size.width, viewController.view.frame.size.height - heightOfMyCustomView);
}
when I push a controller, the top part of the controller's view is hidden by my custom view I added to the navigation bar.
How can I put the controller's view down below my custom view?
I'd also suggest to take a look at UINavigationItem.titleView property, that gives you an ability to provide your view for navigation bar title area.
In your custom UINavigationController init method assign delegate to itself, like this:
CustomeNavCtrl.m file:
#implementation CustomeNavCtrl
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.delegate = self;
}
return self;
}
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
//Todo:viewController.view layout
//Todo:send MyCustomView to back or something other you like.
}
CustomeNavCtrl.h file:
#interface CustomeNavCtrl : UINavigationController<UINavigationControllerDelegate> {
}

When trying to segue, nothing happens

I have an app I am working on, and I am trying to have the logo on for a bit longer, and fade out/slide out/effect when it's done.
Here's my setup:
The Tab Bar controller is not letting me place an Image View inside it, so I created a view to have it on.
I am trying to have the logo stay on for a bit, fade out, then automatically switch the view (Segue) to the Tab Bar controller.
This is what I get out of it: http://youtu.be/l4jL0BfpR2k
So here's my code:
//
// BDNLogoViewController.m
// Bronydom Network
//
// Created by name on 10/1/13.
// Copyright (c) 2013 name. All rights reserved.
//
#import "BDNLogoViewController.h"
#import "BDNTabBarController.h"
#import "BDNFirstViewController.h"
#interface BDNLogoViewController ()
#end
#implementation BDNLogoViewController
- (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.
[UIView animateWithDuration:1 animations:^{
_imageview.alpha = 0;
}];
//BDNTabBarController *viewController = [[BDNTabBarController alloc] init];
//[self.navigationController pushViewController:viewController animated:YES];
(void)#selector(seguePerform:);
}
- (void)seguePerform:(id)sender
{
//BDNTabBarController *myNewVC = [[BDNTabBarController alloc] init];
// do any setup you need for myNewVC
[self performSegueWithIdentifier:#"open" sender:sender];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
Yes, "open" is defined as the segue id.
Do you guys have any ideas on how I could fix this?
To fix, add this
- (void) viewDidAppear:(BOOL)animated
{
[super viewDidAppear:YES];
[self performSegueWithIdentifier:#"open" sender:self];
}
Remove this from your code
(void)#selector(seguePerform:);
// and all other unnecessary segue stuff you had

Resources