UIButton setHidden not working now? - xcode

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"];

Related

Search Bar has wider frame at viewDidLoad

I created UISearchController programmatically and I had set frame for searchBar that I added in it. But the searchBar has wider frame and once I tap on it and tap on the cancel button with search bar; it regains the correct frame. How to set it correct at viewDidLoad?
Code is as such:
searchBar = [[UISearchBar alloc] initWithFrame:CGRectMake(0, 0, 310, 44)];
searchControllerMain = [[UISearchController alloc] initWithSearchResultsController:self.searchResultController];
searchControllerMain.searchBar.delegate = self;
searchControllerMain.searchResultsUpdater = self;
[searchControllerMain.searchBar sizeToFit];
[searchControllerMain.searchBar setPlaceholder:#"events"];
searchControllerMain.searchBar.barTintColor = [UIColor clearColor];
searchControllerMain.searchBar.searchBarStyle = UISearchBarStyleMinimal;
[[UITextField appearanceWhenContainedIn:[UISearchBar class], nil] setBackgroundColor:[UIColor lightGrayColor]];
[self.searchResultController.tableView reloadData];
UIView *searchBarContainer = [[UIView alloc] initWithFrame:searchBar.frame];
[searchBarContainer addSubview:searchControllerMain.searchBar];
self.navigationItem.rightBarButtonItem= [[UIBarButtonItem alloc] initWithCustomView:searchBarContainer];
Here's how I place a searchBar in the navigation item's titleView.
self.searchBar = [UISearchBar new];
self.searchBar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.searchBar.searchBarStyle = UISearchBarStyleMinimal;
self.searchBar.placeholder = #"Search gospel events";
self.searchBar.delegate = self;
self.navigationItem.titleView = self.searchBar;
As you can see, I don't set a frame. It starts off with the right initial size, and will adjust sizes as orientation changes.
Have you tried setting an explicit autoresizingMask? I've never tried placing it in a custom bar button item, so I'm not sure if that will also fix your custom container issue.
Try this:
-(void)viewDidLayoutSubviews{
[super viewDidLayoutSubviews];
[searchControllerMain.searchBar setFrame:CGRectMake(0, 0, 310, 44)];
[searchControllerMain.searchBar setBounds:CGRectMake(0, 0, 310, 44)];
}
It worked for me!!

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!

Active buttons in scrollview

I have a source code where the main screen has 10 buttons (images buttons), each button is linked to sql database with tableview items, in the XIB I select which tag for each button, 1, 2, 3, 4..
I want make these buttons to scroll, how to do so?
I tried to convert the view into scrollview, then I have deleted the images, and created buttons in the scrollview, but now I do not know how to link each of the button to its tag in the tableview?
below is what I used:
#implementation mainViewController;
#synthesize master;
#synthesize model;
(void)viewWillAppear:(BOOL)animated{
UIScrollView *scrollview=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,320,480)];
scrollview.showsVerticalScrollIndicator=YES;
scrollview.scrollEnabled=YES;
scrollview.userInteractionEnabled=YES;
[self.view addSubview:scrollview];
scrollview.contentSize = CGSizeMake(320,960);//You Can Edit this with your require
scrollview.pagingEnabled=YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
UIImageView* imageview1 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"c1#2x.png"]];
imageview1.frame=CGRectMake(18.0, 18, 285,50);
[scrollview addSubview:imageview1];
UIImageView* imageview2 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"c2#2x.png"]];
imageview2.frame=CGRectMake(18.0, 78, 285,50);
[scrollview addSubview:imageview2];
UIImageView* imageview3 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"c3#2x.png"]];
imageview3.frame=CGRectMake(18.0, 138, 285,50);
[scrollview addSubview:imageview3];
UIImageView* imageview4 =[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"c4#2x.png"]];
imageview4.frame=CGRectMake(18.0, 198, 285,50);
[scrollview addSubview:imageview4];
Assingn a tag to the button when you create it:
[buttonName setTag:1];
[buttonName addTarget:self action:#selector(aMethod:) forControlEvents:UIControlEventTouchDown];
Then use:
-(void)aMethod:(UIButton *)button {
if ([button tag] == 1) {
...do something...
}
}
Hope this helps.

Button action is not called on UIViewController added on Paging

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];

Resources