TabBarController didSelectViewController not working - objective-c

I know this is a very repeat topic, but I can't get it works.
MainTab.h:
#import <UIKit/UIKit.h>
#interface MainTab : UITabBarController<UITabBarControllerDelegate, UITabBarDelegate> {
IBOutlet UITabBarController *tabController;
}
#property (nonatomic,retain) IBOutlet UITabBarController *tabController;
#end
MainTab.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSLog(#"main tab");
[super viewDidLoad];
self.tabBarController.delegate = (id)self;
[self setDelegate:self];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"selected %d",tabBarController.selectedIndex);
}
I can't find what I'm missing, any help will be appreciated.
Now I try to link it into MainStoryBoard:
But it doesnt work, what are the connection?

On the basis of your #interface (and your subsequent screen snapshot), MainTab is the UITabBarController, so the following line:
self.tabBarController.delegate = (id)self;
Should just be:
self.delegate = self;
You don't want a tabBarController property in the UITabBarController, itself, nor do you want to use the self.tabBarController syntax. You only use that syntax if you're trying to reference the tab bar controller from one of its children controllers. When in the tab bar controller itself, just refer to self.
Thus, it work if MainBar is defined as:
// MainBar.h
#import <UIKit/UIKit.h>
#interface MainBar : UITabBarController
#end
And
// MainBar.m
#import "MainBar.h"
#interface MainBar () <UITabBarControllerDelegate>
#end
#implementation MainBar
- (void)viewDidLoad
{
[super viewDidLoad];
self.delegate = self;
}
-(void) tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
NSLog(#"selected %d",tabBarController.selectedIndex);
}
#end
And don't forget to set the class of the tab bar controller:
The connections inspector (where I didn't touch a thing) looks like:

Related

presentViewController with storyboards shows black view iOS 7.1 xcode 5.1

i did search SO and the web didn't find amy answer .
i created in storyboard main UITableViewController called A which have simple button.
and another ViewController Called B which have webView and close button.
that is not connected in any form to the main UITableViewController A
now i like to open the B viewController form A and then close the B ViewController with its own close button .
but what ever i try the B view controller is black and blank .
ViewController B ( the popup view )
#import "TAFBLoginDialogViewController.h"
#interface TAFBLoginDialogViewController ()
#end
#implementation TAFBLoginDialogViewController
-(id)initWithAppId:(NSString *)apiKey
requestPremision:(NSString *)requestPremision
delegate:(id<TAFBLoginDialogViewdelegate>)delegate
storyBoardName:(NSString*) storyBoardName
{
//self = [super initWithNibName:#"FBLoginDialog" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"FBLoginDialog"];
if (self) {
// Custom initialization
self.apiKey = apiKey;
self.requestPremision = requestPremision;
self.delegate = delegate;
}
return self;
}
- (IBAction)closeTap:(id)sender
{
[self.delegate closeTaped];
}
-(void)login
{
}
-(void)logout
{
}
-(void)checkForAccessToken:(NSString*)urlString
{
}
-(void)checkLoginRequired:(NSString*)urlString
{
[self.delegate displayRequired];
}
- (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.
}
UITableViewController A ( the main view controller from which i try to open B)
#import "TAFMETableViewController.h"
#interface TAFMETableViewController ()
{
}
#end
#implementation TAFMETableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void) awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cell;
}
- (IBAction)handleOpenFBDialog:(id)sender {
UIStoryboard * storyboard = self.storyboard;
NSString * storyboardName = [storyboard valueForKey:#"name"];
self.appId = #"11111";
self.permissions =#"public_stream";
if(_loginDialogView ==nil)
{
self.LoginDialogViewController = [[TAFBLoginDialogViewController alloc] initWithAppId:_appId
requestPremision:_permissions
delegate:self storyBoardName:storyboardName];
self.loginDialogView = _LoginDialogViewController.view;
}
[self.LoginDialogViewController checkLoginRequired:#"tst"];
NSLog(#"Click!");
}
-(void)accessTokenFound:(NSString*)accessToken
{
NSLog(#"accessTokenFound Click!");
}
-(void)displayRequired
{
NSLog(#"displayRequired Click!");
[self presentViewController:_LoginDialogViewController animated:YES completion:nil];
}
-(void)closeTaped
{
NSLog(#"closeTaped Click!");
[self dismissViewControllerAnimated:NO completion:nil];
}
#end
header file :
#protocol TAFBLoginDialogViewdelegate
-(void)accessTokenFound:(NSString*)accessToken;
-(void)displayRequired;
-(void)closeTaped;
#end
#interface TAFBLoginDialogViewController : UIViewController<UIWebViewDelegate>
{
//ivars
// UIWebView *_webview;
// NSString* _apiKey;
// NSString* _requestPremision;
// id <TAFBLoginDialogViewdelegate> _delegate;
}
#property (retain) IBOutlet UIWebView *webView;
#property (copy) NSString *apiKey;
#property (copy) NSString *requestPremision;
#property (assign) id<TAFBLoginDialogViewdelegate> delegate;
-(id)initWithAppId:(NSString*)apiKey
requestPremision:(NSString*)requestPremision
delegate:(id<TAFBLoginDialogViewdelegate>)delegate
storyBoardName:(NSString*) storyBoardName;
- (IBAction)closeTap:(id)sender;
-(void)login;
-(void)logout;
-(void)checkForAccessToken:(NSString*)urlString;
-(void)checkLoginRequired:(NSString*)urlString;
#end
I have button in the TableView A that trigger :
- (IBAction)handleOpenFBDialog:(id)sender
then this function init the ViewController B
and the invoking :
[self.LoginDialogViewController checkLoginRequired:#"tst"];
which supposed to show the ViewController B
but all it does shows black screen .
in your initWithAppID method you instantiate your view controller from the storyboard and then don't do anything with it. Then you have an if (self) initialization block but you've never initialized self.
It looks like what you want to do is set self equal to the view controller that you instantiated from the storyboard and then set the properties on it.
It might make more sense to make this a class method instead since your allocating a view controller object and inside it's init method your creating another one from the storyboard and ignoring the one you allocated.
+(instancetype) TAFBLoginDialogViewControllerWithAppID:(NSString *)apiKey
requestPremision:(NSString *)requestPremision
delegate:(id<TAFBLoginDialogViewdelegate>)delegate
storyBoardName:(NSString*) storyBoardName
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
TAFBLoginDialogViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"FBLoginDialog"];
// Custom initialization
viewController.apiKey = apiKey;
viewController.requestPremision = requestPremision;
viewController.delegate = delegate;
return viewController;
}

Passing data between two viewcontrollers in xcode using storyboard

I am super new to programming in general. I am trying to make a very simple app.
I am trying to pass data from one viewcontroller to another using a storyboard. On my first screen I have a text field and a button then after you press the button i want the second screen's label to be updated with whatever text you put in the text field.
Here is some of my code:
#import "FirstViewController.h"
#import "SecondViewController.h"
#interface FirstViewController ()
#end
#implementation FirstViewController
#synthesize secondviewData;
- (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)passData:(UIButton *)sender {
// SecondViewController *second = [[SecondViewController alloc] initWithNibName:nil bundle:nil];
self.secondviewData = secondviewData;
secondviewData.passedValue = input.text;
// [self presentViewController:second animated:YES completion:nil];
homeLabel.text = input.text;
}
#end
#import "SecondViewController.h"
#import "FirstViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController
#synthesize passedValue;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
label.text = passedValue;
[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.
}
#end
Here's a link to my code: http://oberober.com/passingData/
So far the label on the second screen keeps going blank no matter what I enter on the first screen's text field.
I feel like I am missing an important concept? Any tips or tutorials I can look at?
Thanks ahead of time,
Casper
You pass the data by using the prepareForSegue method of the first controller. Here's an example from one of my apps that shows a vehicle object being passed to the second controller.
CHRBodyViewController is the class of the second controller.
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
CHRBodyViewController *body = [segue destinationViewController];
body.vehicle = _vehicle;
body.updated = YES;
}

property implementation must have its declaration in interface error

"property implementation must have its declaration in interface" error
I have this error coming up and I am new to xcode so I am unsure what to do.
I had lots of code in a class, then i moved it to a different class and now I have numerous errors. Which dont make sense, as everything looks as it should.
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize MenuScreenButton;
#synthesize HowToPlayButton;
- (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, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(IBAction)HowToPlayButtonPressed:(id)sender
{
HowToPlayViewController *HowToPlayView=[[HowToPlayViewController alloc]initWithNibName:nil bundle:nil];
HowToPlayView.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:HowToPlayView animated:YES completion:NULL];
}
-(IBAction)MenuScreenButtonPressed:(id)sender
{
MenuScreen *MenuScreenView=[[MenuScreen alloc]initWithNibName:nil bundle:nil];
MenuScreenView.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
[self presentViewController:MenuScreenView animated:YES completion:NULL];
}
#end
Below is the .h file:
#import <UIKit/UIKit.h>
#import "HowToPlayViewController.h"
#import "MenuScreen.h"
#interface ViewController : UIViewController
{
IBOutlet UIButton *HowToPlayButton;
IBOutlet UIButton *MenuScreenButton;
}
#property(retain,nonatomic) IBOutlet UIButton *MenuScreenButton;
#property(retain,nonatomic) IBOutlet UIButton *HowToPlayButton;
-(IBAction)HowToPlayButtonPressed:(id)sender;
-(IBAction)MenuScreenButtonPressed:(id)sender;
#end

Missing context for method declaration?

I am trying to create an app and when I added this IBAction, it gave me the error Missing context for method declaration. I have looked up how to fix this but I am a bit confused since I am a beginner. The errors appear in my .m file:
#import "QRscannerSecondViewController.h"
#end
#interface QRscannerSecondViewController ()
#end
#implementation QRscannerSecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.title = NSLocalizedString(#"Info", #"Info");
self.tabBarItem.image = [UIImage imageNamed:#"second"];
}
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
-(IBAction)button1
{
[input resignFirstResponder];
l11 = input.text;
line11.text = [[NSString alloc] initWithFormat:#"%2.f", l11];
}
but I though that it would help if I included my .h file, also.
#import <UIKit/UIKit.h>
#interface QRscannerSecondViewController : UIViewController
#end
#interface TextGameViewController : UIViewController
{
IBOutlet UITextField *input;
IBOutlet UILabel *line11;
NSString *l11;
}
-(IBAction)button1;
You need to put the code:
-(IBAction)button1
{
[input resignFirstResponder];
l11 = input.text;
line11.text = [[NSString alloc] initWithFormat:#"%2.f", l11];
}
before the #end.

Passing data from controllerA to controllerB using delegate

I am trying to send simple data from viewControllerA to viewControllerB using delegate but can not find The problem in my code.here is the code
in ViewControllerA.h
#import <UIKit/UIKit.h>
#class ViewControllerA;
#protocol viewControllerADelegate <NSObject>
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item;
#end
#interface ViewControllerA : UIViewController
- (IBAction)buttonPressed:(id)sender;
#property (assign) id<viewControllerADelegate> delegate;
#end
inViewControllerA.m
#import "ViewControllerA.h"
#interface ViewControllerA ()
#end
#implementation ViewControllerA
#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.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)buttonPressed:(id)sender {
[self.delegate addItem:self data:#"this is data"];
}
#end
and here is ViewControllerB.h
#import <UIKit/UIKit.h>
#import "ViewControllerA.h"
#interface ViewControllerB : UIViewController<viewControllerADelegate>
#end
and ViewControllerB.m
#import "ViewControllerB.h"
#import "ViewControllerA.h"
#interface ViewControllerB ()
#end
#implementation ViewControllerB
- (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.
ViewControllerA *vba = [[ViewControllerA alloc]init];
[vba setDelegate:self];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
-(void) addItem: (ViewControllerA *)controller data:(NSString *)item{
NSLog(#"delegate function called");
}
#end
The implemented function -(void) addItem: (ViewControllerA *)controller data:(NSString *)item is never called.Am I missing something ? Thanks in advance
Are you using storyboards? If yes, you just push the wrong button: storyboard initializes view controllers on its own, so yours code:
ViewControllerA *vba = [[ViewControllerA alloc]init];
[vba setDelegate:self];
just setting delegate on some unused view controller.
Use segues, don't try to reinvent a wheel.