Bug when trying to create view with 2 UIButtons - objective-c

I'm attempting to create a view with two UIButtons. The code compiles without any error but the buttons don't have any labels and they can't be clicked.
-(id)initWithTabBar {
if ([self init]) {
self.title = #"Tab1";
self.tabBarItem.image = [UIImage imageNamed:#"promoters.png"];
self.navigationItem.title = #"Nav 1";
}
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button setTitle:#"Button" forState:UIControlStateNormal];
[button addTarget:self action:#selector(facebookAuthorize) forControlEvents:UIControlEventTouchDown];
[button setFrame:CGRectMake(10, 10, 100, 100)];
[self.view addSubview:button];
[button release];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setTitle:#"Button" forState:UIControlStateNormal];
[button2 addTarget:self action:#selector(facebookLogin) forControlEvents:UIControlEventTouchDown];
[button2 setFrame:CGRectMake(110, 10, 100, 100)];
[self.view addSubview:button2];
[button2 release];
return self;
}

Don't release those buttons. They are created as autoreleased objects. As described in documentation:
You only release or autorelease
objects you own. You take ownership of
an object if you create it using a
method whose name begins with “alloc”,
“new”, “copy”, or “mutableCopy”
None of these words appear in buttonWithType:, so you don't take responsibility and you are safe to assume it's autoreleased.

As mentioned earlier, the buttons cant be named when it is withType. The code to make the words appear in the uibutton is:
UIButton *someButton=[[[UIButton alloc]initWithFrame:CGRectMake(140, 6, 175, 36)]autorelease];
[someButton addTarget:self action:#selector(facebookLogin) forControlEvents:UIControlEventTouchDown];
[someButton setTitle:#"Button" forState:UIControlStateNormal];
[self.view addSubview:someButton];

Related

extra button in rightbarbuttonitems not showing

I create two UIBarButtonItem in the code below. Later I add them to rightBarButtonItems yet only one of the two buttons shows up (the one I put first on the list).
I thought perhaps that the title's view (which is not set) would be in front of the rightBarButtonItems, since the Apple docs says "If there is not enough room to display all of the items in the array, those that would overlap the title view (if present) or the buttons on the left side of the bar are not displayed."
But that doesn't seem to be it either, since the view's frame = {0,0}{0,0}
I can't figure out what I'm doing wrong. Can anybody tell me how to get all rightBarButtonItems to show? Code:
UIBarButtonItem *shareButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(shareAction:)];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *wishlistButton = [[UIBarButtonItem alloc] initWithCustomView:button];
[self.navigationItem setRightBarButtonItems:[[NSArray alloc] initWithObjects:shareButton, wishlistButton, nil]];
Use UIBarButtonItem:
UIBarButtonItem *button = [UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"custom-bar-button.png"] style:UIBarButtonItemStylePlain target:self action:#selector(barButtonTapped:)];
instead of:
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(0, 0, 30, 30)];
[button setBackgroundImage:[UIImage imageNamed:#"custom-bar-button.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(barButtonTapped:) forControlEvents:UIControlEventTouchUpInside];

Remove buttons and labels from SuperView?

In .m File
#interface ReaderViewController ()
{
UIButton *button;
}
Then I create
Two buttons using the button Object
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=-1;
[button addTarget:self
action:#selector(record:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Rec/Stop" forState:UIControlStateNormal];
button.frame = CGRectMake(215, 110, 80, 50);
[self.view addSubview:button];
Then again I am creating one more button usingbutton object
button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.tag=1;
[button addTarget:self
action:#selector(record:) forControlEvents:UIControlEventTouchUpInside];
[button setTitle:#"Rec/Stop" forState:UIControlStateNormal];
button.frame = CGRectMake(340, 110, 80, 50);
[self.view addSubview:button];
Now I want to remove both buttons from superView??
How can i do that
I've tried this it didn't work [button removeFromSuperview];
Try this:
for (UIView* subV in self.view.subviews) {
if ([subV isKindOfClass:[UIButton class]])
[subV removeFromSuperview];
}
NB: this remove all buttons into you superview
Set meaningful tag values for the buttons:
button.tag = 1000;
and
button.tag = 1001;
and then you can remove the buttons with:
[[self.view viewWithTag:1000] removeFromSuperview];
[[self.view viewWithTag:1001] removeFromSuperview];
NOTE: You don't need the instance variable button; you can simply use a local variable instead.

Objective-c : action:#selector(xxxxxx) not called

I'm currently trying to add a button to my app. If the user push on it, it should display an alert.
I want to add the button to a section header of a tableview. Here is my code in tableView:viewForHeaderInSection:
UIImageView *headerView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
...
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
return headerView;
And in the same class, I have the method :
-(void)showMyAlert{
NSLog(#"HERE SHOW ALERT");
...
}
The button is correctly displayed but the showMyAlert method is never called when I push on it.
Does anyone have an idea of what is wrong with my code ?
Thanks if you could help me :)
Change your UIImageView as UIView and return it as the header view.
UIView *headerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
Make sure img is not nil. If that is nil, your button will have no image and you will see nothing.
Modified below lines of code, Try like this:-
[button addTarget:self action:#selector(showMyAlert:) forControlEvents:UIControlEventTouchUpInside];
-(IBAction)showMyAlert:(id)sender{
NSLog(#"HERE SHOW ALERT");
...
}
Try this, in viewDidLoad,
-(void)viewDidLoad
{
UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(XXX, YYY, XXX, YYY)];
UIImageView *imageView = [[[UIImageView alloc] initWithFrame:CGRectMake(0, 0, tableView.bounds.size.width, 20)] autorelease];
[headerView addSubview:imageView];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(180, 4, 15, 15)];
UIImage *img=[UIImage imageNamed:#"myimg"];
[button setImage:img forState:UIControlStateNormal];
[button addTarget:self action:#selector(showMyAlert) forControlEvents:UIControlEventTouchUpInside];
[headerView addSubview:button];
self.tableView.tableHeaderView = headerView;
}

Why are UIButton actions not being called when UIButton is inside a UIScrollView

I have created a springboard like panel containing buttons as follows:
-(void)configurePanel{
self.criteriaPanel.scrollEnabled=YES;
self.criteriaPanel=[[UIScrollView alloc] initWithFrame:CGRectMake(
0.0f,
0.0f,
[UIScreen mainScreen].bounds.size.width,
(BUTTON_HEIGHT+(3*BUTTON_Y_OFFSET))
)];
UIView *scrollViewContent=[[UIView alloc]init];
NSArray *criteriaTypeButtonTitles=[NSArray arrayWithObjects:
#"Option1",
#"Option2",
#"Option3",
#"Option4",
#"Option5",
#"Option6",
nil
];
for (int i=0; i<[criteriaTypeButtonTitles count]; i++) {
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.tag=i;
button.frame = CGRectMake(
(((BUTTON_X_OFFSET*i)+(BUTTON_WIDTH*i))+BUTTON_X_OFFSET)
, BUTTON_Y_OFFSET
, BUTTON_WIDTH
, BUTTON_HEIGHT
);
[button setTitle:[criteriaTypeButtonTitles objectAtIndex:i] forState:UIControlStateNormal];
[button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted];
button.titleLabel.font=[UIFont systemFontOfSize:13.0f];
[button setBackgroundColor:[UIColor grayColor]];
[button.layer setCornerRadius:10.0f];
[button.layer setBorderWidth:1.0f];
[button addTarget:self action:#selector(sortByButtonTap:) forControlEvents:UIControlEventTouchDown];
[scrollViewContent addSubview:button];
}
//based upon # of buttons
self.criteriaPanel.contentSize=CGSizeMake(
(([criteriaTypeButtonTitles count]*BUTTON_WIDTH)+([criteriaTypeButtonTitles count]*BUTTON_X_OFFSET)),
(BUTTON_HEIGHT+(2*BUTTON_Y_OFFSET)));
[self.criteriaPanel addSubview:scrollViewContent];
[self.view addSubview:self.criteriaPanel];
}
The panel displays correctly and scrolls, but the tap events (sortByButtonTap:) for the buttons are never called. I suspect this is related to the buttons being contained in a view which is contained in the scrollview. After reading a number of other questions and the docs I still can't figure out what the solution should be.
EDIT:
I experimented w/ adding the buttons to the UIScrollView (self.criteriaPanel) directly and the button taps call sortByButtonTap: so it is something to do w/ the buttons being in the scrollViewContent.
You might need to set userInteractionEnabled on the containing view scrollViewContent for the button to receive touch events:
[scrollViewContent setUserInteractionEnabled:YES];
What if you try:
[button setExclusiveTouch:YES];
Buttons tend to work abnormally when they are inside a view that also handles touch events.
Follow this code for your query,
Add UIButton in Scrollview
scrollview.pagingEnabled = YES;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame = CGRectMake();
[button setTitle:#"X" forState:UIControlStateNormal];
[button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal];
[button addTarget:self action:#selector(btnCloseClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[button addTarget:self action:#selector(btnCloseDraged:withEvent:) forControlEvents:UIControlEventTouchDragInside];
[button addTarget:self action:#selector(btnCloseDragedEnd:withEvent:) forControlEvents:UIControlEventTouchDragExit];
[scrollview addSubview:button];
scrollview.userInteractionEnabled = YES;
you can put this three methods of button manually.
Let me know if you still have query.
instead of:
[button addTarget:self
action:#selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchDown];
try:
[button addTarget:self
action:#selector(sortByButtonTap:)
forControlEvents:UIControlEventTouchUpInside];
I think your problem is just that you set the wrong UIControlEvent to the UIButton. Change from UIControlEventTouchDown to UIControlEventTouchUpInside. If you connect a UIButton inside the Interface Builder it will always set it to this UIControlEvent:
Another reason:
Otherwise your UIScrollView controls the interactions because it needs to know if you want to scroll it. If you place UIButtons inside your UIScrollView they will be accessible but always with a little delay. This is why you should maybe change the settings for the UIScrollView:
You can disable delays content touches and UIButtons will be accessible immediately

Programmatically Create Button to URL

I've managed to create a simple rounded rect button in Xcode through code but I'm having a little trouble actually getting it to do more what I want.
Ideal situation is a custom button type with image in both states as image.png linking to a URL (say www.google.com) can anyone help me out? I presume I need to write a method to do the link. That I can handle, just not sure how to hook it up without Interface Builder (targetting iOS)
Existing Code
UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
[btn addTarget:self action:#selector(supporturl) forControlEvents:UIControlEventTouchDown];
btn.frame = CGRectMake(20, 20, 150, 50);
[btn setTitle:#"Support and Help" forState:UIControlStateNormal];
[self addSubview:btn];
UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(20.0f, 20.0f, 150.0f, 50.0f)];
[btn setTitle:#"Support and Help" forState:UIControlStateNormal];
[btn setImage:[UIImage imageNamed:#"image.png"] forState:UIControlStateNormal];
[btn addTarget:self action:#selector(openWebPage) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
[btn release];
and in function
-(void) openWebPage
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}