iOS 5: troubles with UITabBarControllerDelegate - ios

Header file:
#import <UIKit/UIKit.h>
#interface PFXTabControllerViewController : UITabBarController <UITabBarControllerDelegate>
#end
Implementation file:
#import "PFXTabControllerViewController.h"
#interface PFXTabControllerViewController ()
#end
#implementation PFXTabControllerViewController
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSLog(#"tabBar didSelectItem");
}
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
NSLog(#"tabBarController didSelectViewController");
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tabBarController.delegate = self; // Tried it both with and without this.
}
The UITabBarcontroller shows just fine, with the tab bar. Clicking on a tab fires didSelectItem but it does not fire didSelectViewController which is the event I need to handle. The UITabBarcontroller is loading and showing different subclasses of UIViewController.
UPDATE Changing that self.tabBarController.delegate = self line to self.delegate = self works, and both fire. I don't understand why this is necessary. Setting self.delegate = self seems... silly. Any ideas?

Just change the header as follows :
#import <UIKit/UIKit.h>
#interface PFXTabControllerViewController : **UIViewController** <UITabBarControllerDelegate>
#end

Related

unwind segue for multiple views

I am working on an app in Xcode, using modal segues to navigate forwards and using unwind segues to go back to the previous view and also to the home view. However, the segue is just not working when I open up the simulator, no errors are shown and I have used the same approach to unwind segue as is used in the Apple To-do list tutorial, so not sure why this isn't working.
Below is my .h file for one of the views I want to unwind from:
#import <UIKit/UIKit.h>
#interface XYZBonBonoftheDayViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIWebView *BBoD;
- (IBAction)unwindBBoD:(UIStoryboardSegue *)segue;
#end
Here is my .m file from the same view:
#import "XYZBonBonoftheDayViewController.h"
#interface XYZBonBonoftheDayViewController ()
#end
#implementation XYZBonBonoftheDayViewController
- (IBAction)unwindBBoD:(UIStoryboardSegue *)segue
{
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
#end
If anyone could help, this would be immensely helpful! Thanks in advance :)

Change UILabel text from subview using delegate

I have a view with a label in it. The lower side of this view is filled with a subview which has it’s own view controller. This subview has a button. My goal: I want to change the label by pressing the button in the subview.
I have tried the code beneath. It works fine when I let the ViewWillAppear method of the delegate call the ‘doSomeTask’ method of the SubviewController. A timer begins to count and after a few seconds, the SubviewController calls the delegates changeLabel method, and the label is changed. However, when I call the ‘doSomeTask’ method manually by pressing the button, the delegate isn’t listening. The call doesn’t reach the changeLabel method.
Probably I am doing something ridiculously stupid here. Forgive me, I’m just learning iOS programming. Thanks in advance for all the feedback.
MyViewController.h
#import <UIKit/UIKit.h>
#import "MySubViewController.h"
#interface MyViewController : UIViewController
#property (strong, nonatomic) IBOutlet UIView *detailView;
#property (strong, nonatomic) IBOutlet UILabel *theLabel;
- (IBAction)changeLabel:(id)sender;
#end
MyViewController.m
#import "MyViewController.h"
#import "MySubViewcontroller.h"
#interface MyViewController ()
#end
#implementation MyViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MySubViewController *subVC = [[MySubViewController alloc] init];
[self addChildViewController:subVC];
[self.detailView addSubview:subVC.view];
// [subVC setDelegate:self]; (no success trying this)
[subVC didMoveToParentViewController:self];
}
-(void)viewWillAppear:(BOOL)animated
{
MySubViewController *mySubVC = [[MySubViewController alloc] init];
[mySubVC setDelegate:self];
[mySubVC doSomeTask]; // this works fine
}
// delegate method
- (void)changeLabel{
self.theLabel.text = #"Hello!";
}
MySubviewController.h
#import <UIKit/UIKit.h>
#protocol MySubViewControllerDelegate <NSObject>
#required
- (void)changeLabel;
#end
#interface MySubViewController : UIViewController
#property (nonatomic, assign) id delegate;
- (IBAction)changeButton:(id)sender;
- (void)doSomeTask;
- (void)goChangeLabel;
#end
MySubViewController.m
#import "MySubViewController.h"
#interface MySubViewController ()
#end
#implementation MySubViewController
#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)goChangeLabel
{
[[self delegate] changeLabel];
}
- (void)doSomeTask
{
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:#selector(goChangeLabel) userInfo:nil repeats:NO];
}
- (IBAction)changeButton:(id)sender {
// [self doSomeTask]; (no succes trying this)
}
#end
Your problem is this:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
MySubViewController *subVC = [[MySubViewController alloc] init];
[self addChildViewController:subVC];
[self.detailView addSubview:subVC.view];
// [subVC setDelegate:self]; (no success trying this)
[subVC didMoveToParentViewController:self];
}
-(void)viewWillAppear:(BOOL)animated
{
MySubViewController *mySubVC = [[MySubViewController alloc] init];
[mySubVC setDelegate:self];
[mySubVC doSomeTask]; // this works fine
}
In these 2 methods you have created 2 separate variables of type MySubViewController. You have only set the delegate on the variable created inside viewWillAppear.
These are not static classes, singletons or anything else similar. If you create a variable of one type it is in no way linked to the other. The same way you can create multiple NSString's that have no bearing on each other.
Create it like so:
#implementation MyViewController
{
MySubViewController *_subVC;
}
- (void)viewDidLoad
{
[super viewDidLoad];
_subVC = [[MySubViewController alloc] init];
}
now you can use _subVC everywhere in that file as the one reference

Single ADBannerViewDelegate for multiple views

I'm trying to implement simgle AdBanner instance for multiple views in an iOS app. For implementing AdbannerDelegate in a viewController one has to do
bannerview.delegate= self;
where bannerview is an instance of AdBannerView. This delegate method however has to be implemented in every viewController which amounts up to a lot of repeating code. How can I make up a simple class that implements all delegate methods and then I call use them in every viewController.
I think the viewControllers you are using are subclasses of UIViewController.
And you are saying all the viewControllers have the same delegate methods.
So,what i want to do is create new ViewController class (UIDelgateViewController) by SubClassing UIViewController and add all delegate methods there , and have all the other viewControllers subclass UIDelgateViewController.
The code goes like this,
.h file->
#interface UIDelegateViewController : UIViewController<ADBannerViewDelegate>
#property ADBannerView *bannerView;
#end
.m file ->
#import "UIDelegateViewController.h"
#interface UIDelegateViewController ()
#end
#implementation UIDelegateViewController
- (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.
_bannerView = [[ADBannerView alloc] init];
_bannerView.delegate =self;
}
-(void)bannerDelegateMethod{
}
Now your Some viewController ->
#import "UIDelegateViewController.h"
#interface SomeViewController : UIDelegateViewController
#end
#import "SomeViewController.h"
#interface SomeViewController ()
#end
#implementation SomeViewController
- (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.
[self.view addSubview:self.bannerView];
self.bannerView.frame = ..../
}
If you want to keep always on the screen the same banner while navigating and changing views, you should consider to use View Controller containtment API
A great example is that remarkable sample code written by Apple, that shows how to keep the same banner instance while moving in a tabbar or navigation controller. It could be also a great start for you project.

how to access function one class to another objective c [duplicate]

This question already has answers here:
Accessing Method from other Classes Objective-C
(8 answers)
Closed 9 years ago.
This is my viewcontroller1.m buttons userinteractionenabled function,
-(void) BtnUserInteractionStatus {
oneBtn2.userInteractionEnabled = NO;
oneBtn3.userInteractionEnabled = NO;
}
- (void)viewDidLoad {
[super viewDidLoad];
[self BtnUserInteractionStatus];
}
This is my viewcontroller2.m buttons userinteractionenabled function access from viewcontroller1.m,
#import "ViewController1.h"
- (void)viewDidLoad {
[super viewDidLoad];
svc = [[StageViewController alloc]init];
[svc BtnUserInteractionStatus];
}
my viewcontroller2.h,
#import "ViewController1.h"
#interface ViewController2 : UIViewController {
StageViewController *svc;
}
#property (assign) StageViewController *svc;
But, this is not working. If there is any wrong please correct it.
This will be your ViewController2.m
#import "ViewController1.h"
- (void)viewDidLoad
{
[super viewDidLoad];
svc = [[ViewController1 alloc]initWithNibName:#"ViewController1" bundle:nil];
[self.view addSubview:svc.view];
}
ViewController2.h
#import "ViewController1.h"
#interface ViewController2 : UIViewController
{
ViewController1 *svc;
}
ViewController1.m
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
// Custom initialization
[self BtnUserInteractionStatus]; /*Provide your call to BtnUserInteractionStatus within init function*/
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
-(void)BtnUserInteractionStatus
{
oneBtn2.userInteractionEnabled = NO;
oneBtn3.userInteractionEnabled = NO;
}
Whenever you want the object library elements of a viewController loaded from another ViewContoller you need to define its xib and then add its corresponding view as either a subview of the present viewcontroller or push the viewcontrller from the current ViewController .Simply calling the BtnUserInteractionStatus method using its currently created instance wont help..
Hope it Helps !!

view did load but not viewDidLoad

I have two view controllers: BSViewController and BSotherViewController. BSViewController has a button on it that is segues to BSotherViewController. That part works fine and I can see the one VC and then the "other" VC when I run on the simulator. But I have an NSLog(#"other"); in BSotherViewController's -viewDidLoad which is not showing in the console. A similar NSLog(#"other"); in BSViewController's -viewDidLoad which is showing in the console. I suspect the problem is the lack of an IBAction but I cannot figure where it should go and how it should be linked in the storyboard.
BSViewController.h
#import <UIKit/UIKit.h>
#interface BSViewController : UIViewController
#property (nonatomic) NSInteger number;
#end
BSViewController.m
#import "BSViewController.h"
#interface BSViewController ()
#end
#implementation BSViewController
#synthesize number;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.number = 25;
NSLog(#"number: %d", self.number);
}
#end
BSotherViewController.h
#import <UIKit/UIKit.h>
#interface BSotherViewController : UIViewController
#property (nonatomic) NSInteger anumber;
#end
BSotherViewController.m
#import "BSotherViewController.h"
#include "BSViewController.h"
#interface BSotherViewController ()
#end
#implementation BSotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
NSLog(#"other number:");
[super viewDidLoad];
// Do any additional setup after loading the view.
BSViewController *view = [[BSViewController alloc] init];
self.anumber = view.number;
NSLog(#"other number: %d", self.anumber);
}
In the storyboard is the second view controller set up as a BSotherViewController? You can verify by hovering over the view controller icon in the storyboard.
If it's not, then click it and go to the identity controller and type it in.

Resources