I need to retrieve when a user click over a tabbaritem into a uitabbarcontroller to change something..
here is my code:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
if (item == [tabBarController.tabBar.items objectAtIndex:2]) {
item.title = #"add shot";
}
else
{
item.title = #"Race";
}
}
but If I do this:
self.tabBarController.tabBar.delegate = self;
i receive a sigkill...
what's the right solution? thanks in advance
Does your view controller conform to the UITabBarDelegate protocol?
In the header file:
#interface MyViewController : UIViewController<UITabBarDelegate> {
// ...
}
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item;
#end
Then, just do:
tabBar.delegate = self;
Instead of:
self.tabBarController.tabBar.delegate = self;
And:
- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
//self.tabBarItem.title = #"Title";
}
I came across this answer while learning iOS development, But I wanted to include the little missing pieces for n00bs like me.
// HelloWorldViewController.h
#interface HelloWorldViewController : UIViewController <UITabBarDelegate>
{
}
#property (weak, nonatomic) IBOutlet UITabBar *tabBar;
#end
And
// HelloWorldViewController.m
#interface HelloWorldViewController ()
#end
#implementation HelloWorldViewController
#synthesize tabBar;
- (void) viewDidLoad
{
tabBar.delegate = self;
}
-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
NSLog(#"didSelectItem: %d", item.tag);
}
#end
Related
First of all, let me say that I am new to Objective C.
I'm basically trying to pass the originalPriceOnGraph variable from ViewController (UIViewController) to the original variable from GraphView (UIView). However, I keep getting 0.00 when I try and display original. I don't get what exactly is the problem. Here's some of my code:
GraphView.h
#interface GraphView : UIView
#property (nonatomic) double original;
#end
GraphView.m
#implementation GraphView
#synthesize original;
- (id)initWithFrame:(CGRect)frame
{
//some code here
}
- (void)drawRect:(CGRect)rect;
{
NSLog(#"%.2f", original);
//some more code here
}
#end
ViewController.m
#interface OtherViewController ()
#end
#implementation OtherViewController
#synthesize originalPriceOnGraph;
#synthesize graph;
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
originalPriceOnGraph = 20.00;
graph = [[GraphView alloc] init];
graph.original = originalPriceOnGraph;
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#end
ViewController.h
#interface OtherViewController : UIViewController
#property (strong, nonatomic) GraphView *graph;
#property (nonatomic) double originalPriceOnGraph;
#end
Thank you in advance!
EDIT: I was able to solve this by creating an IBOutlet between the OtherViewController and GraphView. I also got rid of the alloc init statement for GraphView in ViewController.m. Thank you all for your suggestions!
Are you sure the GraphView's drawRect: method isn't getting called before you set its 'original' property?
If so, try initializing any instance of a GraphView with a default value for original.
In GraphView.h:
-(id)initWithOriginal:(double)original;
In GraphView.m:
-(id)initWithOriginal:(double)original
{
self = [super init];
if (self) {
[self setOriginal:original];
}
return self;
}
In ViewController.m:
-(void)viewDidLoad
{
[super viewDidLoad];
originalPriceOnGraph = 20.00;
[self setGraph:[[GraphView alloc] initWithOriginal:originalPriceOnGraph]];
}
use :
self.graph = [[GraphView alloc] init];
self.graph.original = originalPriceOnGraph;
I've hit an "Use of Undeclared Identifier" Error. Seems there could be a lot of reasons for this, and I couldn't see what the problem was with mine. First, here's the code:
#import "CMViewController.h"
#import "CMAEncode.h"
#interface CMViewController ()
#property (weak, nonatomic) IBOutlet UITextView *toCode;
#property (weak, nonatomic) IBOutlet UITextView *Coded;
#property (weak, nonatomic) IBOutlet UISwitch *onOrOff;
- (IBAction)StartEncodeDecode:(id)sender;
#end
#implementation CMViewController
- (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)StartEncodeDecode:(id)sender {
NSString *finishedText;
NSString *toCode = self.toCode.text;
if (self.onOrOff.on == TRUE) {
finishedText = [CMAEncode encodeText:toCode];
self.Coded.text = finishedText;
} else {
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField // Undeclared Identifier here.
{
[textField resignFirstResponder];
return YES;
}
}
#end
I'm trying to make a textfield resign first responder so the keyboard will return. But I really just want to know why this error is occurring, it might help people out with future instances of this error.
You have defined a method inside another method which is not allowed in any language objective-c. Please move textFieldShouldReturn: after StartEncodeDecode: method as below
- (IBAction)StartEncodeDecode:(id)sender {
NSString *finishedText;
NSString *toCode = self.toCode.text;
if (self.onOrOff.on == TRUE) {
finishedText = [CMAEncode encodeText:toCode];
self.Coded.text = finishedText;
} else {
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
I'm studing protocol and delegates using an Example! When I try to re create this example I notice that this condition is not respected:
if([delegate respondsToSelector:#selector(amountEntered:)]) {blabla}
where is the mistake? Scripts:
First View .h
#import <UIKit/UIKit.h>
#import "EnterAmountViewController.h"
#interface DelegateExampleViewController : UIViewController <EnterAmountDelegate>{
IBOutlet UILabel *amountLabel;
}
-(IBAction)changeAmountPressed;
#end
First View .m
#import "DelegateExampleViewController.h"
#implementation DelegateExampleViewController
-(IBAction)changeAmountPressed
{
EnterAmountViewController * enterAmountVC = [[EnterAmountViewController alloc]init];
enterAmountVC.delegate = self;
}
-(void)amountEntered:(NSInteger)amount
{
amountLabel.text = [NSString stringWithFormat:#"%i" , amount];
}
#end
Second View .h
#import <UIKit/UIKit.h>
#protocol EnterAmountDelegate <NSObject>
-(void)amountEntered:(NSInteger)amount;
#end
#interface EnterAmountViewController : UIViewController {
IBOutlet UITextField *amountTextField;
id<EnterAmountDelegate> delegate;
}
-(IBAction)savePressed;
#property(nonatomic,retain) id<EnterAmountDelegate> delegate;
#end
Second View .m
#import "EnterAmountViewController.h"
#import "DelegateExampleViewController.h"
#implementation EnterAmountViewController
#synthesize delegate;
- (void)viewDidLoad {
[super viewDidLoad];
amountTextField.text = #"";
[amountTextField becomeFirstResponder];
}
-(IBAction)savePressed
{
if([delegate respondsToSelector:#selector(amountEntered:)])
{
[delegate amountEntered:[amountTextField.text intValue]];
NSLog(#"rugg");
}
}
#end
Thanks in advance!
In the method:
-(IBAction)changeAmountPressed
{
EnterAmountViewController * enterAmountVC = [[EnterAmountViewController alloc]init];
enterAmountVC.delegate = self;
}
you are creating an instance of EnterAmountViewController on the stack as a local variable. This variable will be inaccessible at the end of the scope. So, when you execute savePressed you are doing it on a different object where you did not set delegate.
In other words, when you check
if([delegate respondsToSelector:#selector(amountEntered:)])
it returns NO because delegate is nil...
The correct way to set the delegate is using the prepareForSegue mechanism:
#implementation DelegateExampleViewController
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
[(EnterAmountViewController*)segue.destinationViewController setDelegate:self];
}
...
You do not need the changeAmountPressed method nor the relative binding.
I'm having 5 ViewControllers in each of that i have to display iAd,so that i have to implement iAd code in each ViewControllers. Instead of that if i create set of common code in AppDelegate means i can just call that code wherever i need iAd to be displayed.
If anyone has implemented this iAd concept means help me to get out of this issue. Thanks in Advance.
Just create a Pointer to iAD in APP delegate.
.h:
#property (strong, nonatomic) ADBannerView *UIiAD;
.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
UIiAD = [[ADBannerView alloc] init];
return YES;
}
Then in your ViewControllers do this:
.h:
#property (strong, nonatomic) ADBannerView *UIiAD;
.m:
- (AppDelegate *) appdelegate {
return (AppDelegate *)[[UIApplication sharedApplication] delegate];
}
- (void) viewWillAppear:(BOOL)animated {
UIiAD = [[self appdelegate] UIiAD];
UIiAD.delegate=self;
// CODE to correct the layout
}
- (void) viewWillDisappear:(BOOL)animated{
UIiAD.delegate=nil;
UIiAD=nil;
[UIiAD removeFromSuperview];
}
Do this for all the view controllers with appropriate code to redesign the layout!
hi this looks like a good answer but don't you have to import the AppDelegate in your m files
#import "AppDelegate.h"
and shouldn't you hide the add if no network connection or add not showing
In the AppDelegate.h:
#import <iAd/iAd.h>
#interface AppDelegate : UIResponder <UIApplicationDelegate>
{
ADBannerView *_bannerView;
...
...
}
#property (nonatomic, retain) ADBannerView *_bannerView;
In the AppDelegate.m:
#synthesize _bannerView;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
...
if ([ADBannerView instancesRespondToSelector:#selector(initWithAdType:)]) {
self._bannerView = [[ADBannerView alloc] initWithAdType:ADAdTypeBanner];
} else {
self._bannerView = [[ADBannerView alloc] init];
}
...
}
In the view controllers that you need to add iAd, create a container UIView with name 'vwAd' and make the connection in xib file where you want to display iAds.
#interface ViewController : UIViewController<ADBannerViewDelegate>
{
IBOutlet UIView *vwAd;
...
...
}
- (void)layoutAnimated:(BOOL)animated
{
CGRect contentFrame = self.view.bounds;
if (contentFrame.size.width < contentFrame.size.height) {
self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierPortrait;
} else {
self.appDelegate._bannerView.currentContentSizeIdentifier = ADBannerContentSizeIdentifierLandscape;
}
CGRect bannerFrame = self.appDelegate._bannerView.frame;
if (self.appDelegate._bannerView.bannerLoaded) {
bannerFrame.origin.y = 0;
} else {
bannerFrame.origin.y = vwAd.frame.size.height;
}
[UIView animateWithDuration:animated ? 0.25 : 0.0 animations:^{
self.appDelegate._bannerView.frame = bannerFrame;
}];
}
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
...
[self.appDelegate._bannerView removeFromSuperview];
self.appDelegate._bannerView.delegate = nil;
self.appDelegate._bannerView.delegate = self;
[vwAd addSubview:self.appDelegate._bannerView];
[self layoutAnimated:NO];
}
Please also review iAdSuite examples from Apple. Original layoutAnimated function can be found in iAdSuite examples. Do not forget to add the delegate functions from iAdSuite example into your view controller.
When calling [polygonShapeView setNeedsDisplay]; my polygonShapeView drawRect method is NOT called. I am able to do polygonShapeView.hidden = YES, which works fine so I have a good reference to the view and have hooked up my outlet. Any ideas?
Controller.h
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "PolygonShape.h"
#import "PolygonShapeView.h"
#interface Controller : NSObject {
IBOutlet UIButton *decreaseButton;
IBOutlet UIButton *increaseButton;
IBOutlet UILabel *numberOfSidesLabel;
IBOutlet PolygonShape *polygonShape;
IBOutlet PolygonShapeView *polygonShapeView;
IBOutlet UILabel *polygonLabel;
}
- (IBAction)decrease:(id)sender;
- (IBAction)increase:(id)sender;
- (void)awakeFromNib;
- (void)updateInterface;
#end
Controller.m
//
// Controller.m
//
// Created by Chris Muench on 6/24/11.
// Copyright 2011 N/A. All rights reserved.
//
#import "Controller.h"
#implementation Controller
- (IBAction)decrease:(id)sender
{
[polygonShape setNumberOfSides:numberOfSidesLabel.text.integerValue - 1];
[self updateInterface];
}
- (IBAction)increase:(id)sender
{
[polygonShape setNumberOfSides:numberOfSidesLabel.text.integerValue + 1];
[self updateInterface];
}
- (void)awakeFromNib
{
polygonShape = [[PolygonShape alloc] initWithNumberOfSides:numberOfSidesLabel.text.integerValue minimumNumberOfSides:3 maximumNumberOfSides:12];
[self updateInterface];
}
- (void)updateInterface
{
[polygonShapeView setNeedsDisplay];
numberOfSidesLabel.text = [NSString stringWithFormat:#"%d",polygonShape.numberOfSides];
polygonLabel.text = polygonShape.name;
if (polygonShape.numberOfSides == polygonShape.maximumNumberOfSides)
{
increaseButton.enabled = NO;
}
else
{
increaseButton.enabled = YES;
}
if(polygonShape.numberOfSides == polygonShape.minimumNumberOfSides)
{
decreaseButton.enabled = NO;
}
else
{
decreaseButton.enabled = YES;
}
}
#end
I can only guess, but it seems your polygonShapeView is not linked in any way to the polygonShape. So it might draw, but not according to the data you expect.
I think there should be something like polygonShapeView.shape = polygonShape; in awakeFromNib, or in updateInterface.