Button action is not called on UIViewController added on Paging - xcode

I'm making one application in which I had added four class on childView
[self addChildViewController:homeScreenViewController];
[self addChildViewController:portFolioViewController];
[self addChildViewController:watchListViewController];
[self addChildViewController:marketingViewController];
Than I call the childView on pageControl as paging.
Now on the homeScreenViewController Class I have added a button but the button do not take any action.
I have declare button like this in HomeScreenViewController class. Here is the code
btn_Trade = [UIButton buttonWithType:UIButtonTypeCustom];
btn_Trade.frame = CGRectMake(0.0, 150.0, 142.0, 40.0);
[btn_Trade setTitle:#"TRADE" forState:UIControlStateNormal];
[btn_Trade setBackgroundColor:[UIColor redColor]];
btn_Trade.titleLabel.font = [UIFont fontWithName:#"SegoeUI-Light" size:20.0];
[btn_Trade setContentHorizontalAlignment:UIControlContentHorizontalAlignmentCenter];
[btn_Trade setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[btn_Trade addTarget:self action:#selector(pushToTradeListViewController) forControlEvents:UIControlEventTouchUpInside];
[btn_Trade setEnabled:YES];
[self.view addSubview:btn_Trade];
Please suggest me.

Can you add the code of your pushToTradeListViewController method please ?
BTW, if your pushToTradeListViewController looks like that :
-(void)pushToTradeListViewController:(id)sender{
....
}
you need to add : (2 points) after your method name in your selector, like this :
[btn_Trade addTarget:self action:#selector(pushToTradeListViewController:) forControlEvents:UIControlEventTouchUpInside];

Related

UINavigationController - change back button to image

ΩI've been trying to do this for like an hour now, nothing seems to work:
Things I've tried:
1) Doesn't work.
UIImage *image = [UIImage imageNamed:#"scr1_button_topbar.png"];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setBackgroundImage: [image stretchableImageWithLeftCapWidth:7.0 topCapHeight:0.0] forState:UIControlStateNormal];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
[button addTarget:self action:nil forControlEvents:UIControlEventTouchUpInside];
UIView *v=[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[v addSubview:button];
UIBarButtonItem *forward = [[UIBarButtonItem alloc] initWithCustomView:v];
self.navigationItem.backBarButtonItem= forward;
2) Works, but looks REALLY stupid, it leaves the "Back" text there.
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"scr2_btnback"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
3) Button doesn't show
[[UIBarButtonItem appearance] setBackButtonBackgroundImage:[UIImage imageNamed:#"scr2_btnback"] forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
And setting the text to #""
4) Setting the left bar button item - Works, but that's a little cheating, I want the back button, not the left button.
UIImage *backImage = [UIImage imageNamed:#"scr2_btnback.png"];
UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
backButton.frame = CGRectMake(0, 0, backImage.size.width, backImage.size.height);
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButton:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *backBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton] ;
self.navigationItem.hidesBackButton = YES;
self.navigationItem.leftBarButtonItem = backBarButtonItem;
Just got told, you can't edit the view of the backBarButtonItem... YEEY me.

Bar button item event not activate in ios 6 or late

I tried this code:
custom barbuttonitem:
-(id)initWithImage:(UIImage *)image title:(NSString*)title target:(id)target action:(SEL)action {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame= CGRectMake(0.0, 0.0, image.size.width, image.size.height);
button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
button.titleLabel.shadowOffset = CGSizeMake(0, -1);
button.titleLabel.shadowColor = [UIColor colorWithWhite:0 alpha:0.5];
button.titleLabel.textColor = [UIColor redColor];
[button setTitle:title forState:UIControlStateNormal];
[button setBackgroundImage:image forState:UIControlStateNormal];
[button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
[view addSubview:button];
self = [[UIBarButtonItem alloc] initWithCustomView:view];
return self;
}
and used it:
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"my_account_icon.png"] title:#"" target:self action:#selector(buttonClick:)];
But in ios 6 event not activate, ios 5 run nomal, please help me !!!!
UIBarButtonItems with a customView set will not fire actions.
Try the following:
-(id)initWithImage:(UIImage *)image title:(NSString*)title target:(id)target action:(SEL)action
{
//create whatever custom views you need
UIView *view =[[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, image.size.width, image.size.height) ];
UITapGestureRecognizer * gestureRec = [[UITapGestureRecognizer alloc] initWithTarget: target action: action];
[view addGestureRecognizer: gestureRec];
//add your custom subviews to the view
self = [[UIBarButtonItem alloc] initWithCustomView:view];
return self;
}
Adding gesture recognizer to the subview of the UIBarButtonItem worked for me, even if UIBarButtonItem is initialized with the custom view.
Good luck!

UIButton setHidden not working now?

I have 10 or so buttons setup inside a method as follows:
#implementation MyViewController
UIButton *originalButton;
etc...
- (void)setupButtons
{
originalButton = [UIButton buttonWithType:UIButtonTypeCustom];
[originalButton addTarget:self action:#selector(originalButtonWasPressed:) forControlEvents:UIControlEventTouchUpInside];
originalButton.frame = CGRectMake(20.0, 30.0, 100.0, 39.0);
UIImage *buttonImage = [[UIImage imageNamed:#"originalreg.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:#"originalregblue.png"] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[originalButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[originalButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[self.view addSubview:originalButton];
etc…
}
I decided to pull the common code into another method for efficiencies:
- (void)setupButton:(UIButton *)myButton withSelector:(SEL)selector withX:(CGFloat)x withY:(CGFloat)y withRegImage:(NSString *)regImage withHighlightImage:(NSString *)highlightImage
{
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self action:selector forControlEvents:UIControlEventTouchUpInside];
myButton.frame = CGRectMake(x, y, 100.0, 39.0);
UIImage *buttonImage = [[UIImage imageNamed:regImage] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
UIImage *buttonImageHighlight = [[UIImage imageNamed:highlightImage] resizableImageWithCapInsets:UIEdgeInsetsMake(18, 18, 18, 18)];
[myButton setBackgroundImage:buttonImage forState:UIControlStateNormal];
[myButton setBackgroundImage:buttonImageHighlight forState:UIControlStateHighlighted];
[self.view addSubview:myButton];
}
…and call it as such:
- (void)setupButtons
{
[self setupButton:originalButton withSelector:#selector(originalButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:#"originalreg.png" withHighlightImage:#"originalregblue.png"];
etc...
}
THIS ALL WORKS except, one of my buttons is used to hide all the others. In the original setup, pressing the "Hide Buttons" button resulted in the other buttons being hidden. Now, they remain on the screen. Here's the code for that:
[self setupButton:hideButtonsButton withSelector:#selector(hideButtonsButtonWasPressed:) withX:20.0 withY:530.0 withRegImage:#"hidebuttonsreg.png" withHighlightImage:#"hidebuttonsregblue.png"];
- (void)hideButtonsButtonWasPressed:(id)sender
{
// hide the buttons
originalButton.hidden = YES;
originalButton.enabled = NO;
etc…
}
I've confirmed this method is being called and the setHidden/setEnabled calls are being executed.
Any pointers gratefully received!
Tony.
its because your method uses single instance all the time. look at the first line of your initialization
myButton = [UIButton buttonWithType:UIButtonTypeCustom];
replace this line with
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
it will return new instance of button each time.
YOu have Declared button instance like following ,right.
IBOutlet UIButton *btn1, *btn2, *btn3;
Now create a new button with your method as you have done before, just assign that button to respected button object
btn1 = [self setupButton:originalButton withSelector:#selector(originalButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:#"originalreg.png" withHighlightImage:#"originalregblue.png"];
btn2 = [self setupButton:originalButton withSelector:#selector(duplicateButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:#"duplicatereg.png" withHighlightImage:#"originalregblue.png"];
btn3 = [self setupButton:originalButton withSelector:#selector(olderButtonWasPressed:) withX:20.0 withY:30.0 withRegImage:#"olderreg.png" withHighlightImage:#"originalregblue.png"];

Change Button Text Color for Button in Footer in Table View

I have a UIView that I've made into a button that's added below my Table View:
myUIView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 120,50)];
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton setAutoResizingMask: [UIViewAutoResizingFlexibleWidth|UIViewAutoResizingFlexibleHeight];
[myButton setFrame:CGRectMake(20,0,80,40)];
myButton.titleLabel.textColor = [UIColor blackColor]; //Doesn't seem to work
[myButton setTitle:#"Test" forState:UIControlStateNormal];
myButton.titleLabel.font = [UIFont boldSystemFontOfSize:18];
[myButton setBackgroundImage:[[UIImage imageNamed:#"myImage.png"] stretchableImageWithLeftCapWidth:10.0 topCapHeight:0.0] forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(myAction:) forControlEvents:UIControlEventTouchUpInside];
[myUIView addSubView:myButton];
No matter what I try, the font always seems to be white. What can I do to change the font's color?
You want to use setTitleColor: forState:
[myButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

InputAccessoryView of a UITextField

I need to stick a toolBar with a UITextField inside to a keyboard.
What if I make the whole toolBar (which I think is the textField's super view) the inputAccessoryView of its textField?
I mean like this:
textField.inputAccessoryView = toolBar; // textField is inside the toolBar
I've been trying on this but I have not made it work yet.
Anyone can help?
Or is there another way to achieve what I want?
- (void)viewDidLoad
{
[self createAccessoryView];
[textField setDelegate:self];
[textField setKeyboardType:UIKeyboardTypeDefault];
[textField setInputAccessoryView:fieldAccessoryView];
}
- (void)createAccessoryView
{
CGRect frame = CGRectMake(0.0, self.view.bounds.size.height, self.view.bounds.size.width, 44.0);
fieldAccessoryView = [[UIToolbar alloc] initWithFrame:frame];
fieldAccessoryView.barStyle = UIBarStyleBlackOpaque;
fieldAccessoryView.tag = 200;
[fieldAccessoryView setBarStyle:UIBarStyleBlack];
UIBarButtonItem *spaceButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(done:)];
UISegmentedControl* segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:NSLocalizedString(#"Previous", #""), NSLocalizedString(#"Next", #""), nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
[segmentedControl setMomentary:YES];
UIBarButtonItem *segmentButton = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[fieldAccessoryView setItems:[NSArray arrayWithObjects:segmentButton, spaceButton, doneButton, nil] animated:NO];
[segmentButton release];
[spaceButton release];
[doneButton release];
[segmentedControl release];
}
When are you setting the property? You may be setting it after the view has already loaded.
Try setting it in
- (void) viewDidLoad

Resources