UIButton with the tag property - iphone

I am getting my UIButton with its tag bay name by using.
UIButton *mybutton=[[UIButton alloc]init];
mybutton=(UIButton*)[cell viewWithTag:5];
and with
UIButton* mybutton=(UIButton*)[cell viewWithTag:5];
This is fine.but when i change it's text(with tag button) by using
[mybutton setTitle:#"hello" forState:UIControlStateNormal];
App Crashes with this error
[UITableViewCell setTitle:forState:]: unrecognized selector sent to instance 0x4b6f540'
* Call stack at first throw:
Any Solution??
Thanks in advance

Use with your contentView
mybutton=(UIButton*)[cell.contentView viewWithTag:5];

Related

How to set picture for uibutton when accessing it as subview

So i have a button in a view i know how to access it but i have no idea how to change picture on that button. Here is what i got:
for(UIView *button in [okvir subviews]){
if([button isKindOfClass:[UIButton class]]){
UIImage *picture=[UIImage imageWithData:slika];
[button setImage:picture forState:UIControlStateNormal];
}
}
However that results in a error:
No visible #interface for 'UIView' declares the selector 'setImage:forState:'
I'm guessing that's because button is UIView not UIButton, any idea how to do that?
You need to cast your pointer so that the compiler recognizes it as being a UIButton and not a generic UIView. This is entirely safe since you are checking the runtime type of your pointed object, so you can be sure it is a UIButton.
Use this:
[(UIButton*)button setImage:picture forState:UIControlStateNormal];

Targetting a UIButton in a UIView, with addTarget

My ViewController.m creates an instance of a myUIView.
In myUIView a UIButton is created.
All seems good, except to capture the button presses I use addTarget, at the ViewController level.
Pressing the button causes a crash, saying: "unrecognized selector sent to instance 0x6c254e0..."
Is this addTarget code wrong? Would appreciate anyone's assistance.
- (void)viewDidLoad{
<UIVIew implementation etc...>
[myUIView.myButton addTarget:self action:#selector(myButtonIsPressed:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)myButtonIsPressed{
NSLog(#"Pressed!");
}
Thanks.
Simply remove : after myButtonIsPressed in the line :
[myUIView.myButton addTarget:self action:#selector(myButtonIsPressed:) forControlEvents:UIControlEventTouchUpInside];

Getting an runtime exception using addTarget:Action:forControlEvents

I am trying to use addTarget:Action:forControlEvents but I am receiving a runtime exception for an unrecognized selector.
This is being called from a UIView subclass in initWithFrame.
Below is my code:
myButton = [[UIButton alloc] initWithFrame:frame];
[myButton addTarget:self action:&selector(displayList:)
forControlEvents:UIControlEventTouchUpInside];
My method:
-(void)displayList:(id)sender
When I click on the button it receive the following message:
-[MyClass displayList:]: unrecognized selector sent to instance.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: -[MyClass displayList:]: unrecognized selector sent to instance.
MyClass is a custom control that is a UIView subclass with a UIButton and UILabel. This class is going to be placed on a ViewController of another application.
I'm not sure what I am missing. Any help would be greatly appreciated.
it should read
[myButton addTarget:self action:#selector(displayList:) forControlEvents:UIControlEventTouchUpInside];
# instead of &
[myButton addTarget:self action:&selector(displayList:) forControlEvents:UIControlEventTouchUpInside];
The syntax is #selector(displayList:)
Also, there is a change your button may not fire the event. Try creating it this way:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; //this will return an autoreleased UIButton object, be careful.
button.frame = CGRectMake(...);
...

added button to uitableviewcell not responding

I have created a custom tableviewcell through interface builder with a label and a button. In the .h and .m file I have made outlets and actions for the button which I have connected.
This cell is added to a tableview controlled by a uiviewcontroller class. However my problem is that when I tap the button, the button is not activated, however I am pushed on to the detailed view belonging to the cell. It seems like the button is behind the cell.
Any suggestions for what I have forgotten or should change?
I have created a button programatically and added as subview to the cell instead and this works, however this gives me another problem as the button is added everytime a cell is loaded.
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(200.0f, 5.0f, 100.0f, 40.0f)];
//Set image
UIImage *img = [UIImage imageNamed:#"myPackage.png"];
[button setImage:img forState:UIControlStateNormal];
[img release];
[button addTarget:self action:#selector(myPackagePushed:) forControlEvents:UIControlEventTouchUpInside];
[cell addSubview:button];
I would really like to use the interface builder approach - any suggestions?
Regards
try
[cell.contentView addSubview:button];
also
UIImage *img = [UIImage imageNamed:#"myPackage.png"]; here you are not allocating memory so no need for
[img release]; because there is a possibility of crash
The button should fire the IBAction method that you have set in IB. Have you checked by inserting an NSLog or by setting a breakpoint if the method is entered?
Is the button set to be initially activated in IB?
The crux with the IB construction is that the IBOutlet gets its value when the cell is loaded from the xib. The IBOutlet will then be the button in the cell that the tableview happend to load last.
WHen you add the button programatically, you might need to have the previous added button to removeFromSuperview before you add a button again to that cell.
In my case, the problem was that the button was a subview of the background view.
Buttons assigned to the background view apparently can't respond to touches.

Create UIView through code and add events in iphone

I have created a UIView and added label to it and latter assign it to Controller.
Now whenever I click on my View it shows me "EXC_BAD_ACCESS”.
Below is my code.
//create a UIView in App Delegate
UIView *viewPtr = [[[UIView alloc] initWithFrame:frmRect] autorelease];
//created a Button and added to UIView
UIButton *btnPointer = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnPointer.frame = cgframe; // provides both a position and a size
[btnPointer setTitle:btnLabelText forState:UIControlStateNormal];
[btnPointer addTarget:self action:#selector(generate:) forControlEvents:UIControlEventTouchUpInside];
[viewPtr addSubview:btnPointer];
//Now need to add this UIView to a controller
viewController.view = viewPtr;
I am able to display the button on the Form but when I click on the form or the button I get
"EXC_BAD_ACCESS”.
You should create the view in the .m file of your view controller like this.
- (void)loadView
{
UIView *viewPtr = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//created a Button and added to UIView
UIButton *btnPointer = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btnPointer.frame = cgframe; // provides both a position and a size
[btnPointer setTitle:btnLabelText forState:UIControlStateNormal];
[btnPointer addTarget:self action:#selector(generate:) forControlEvents:UIControlEventTouchUpInside];
[viewPtr addSubview:btnPointer];
//Now need to add this UIView to a controller
self.view = viewPtr;
[viewPtr release];
}
Hope this helps.
Thanks,
Madhup
Instead of use the autorelease
UIView *viewPtr = [[[UIView alloc] initWithFrame:frmRect] autorelease];
Create the UIView *viewPtr on the .h file, and only do the release in the dealloc method
So, you will have the declaration on your .h file
UIView *viewPtr;
And you .m file will be with the following lines, when you instantiate the viewPtr in the same place where you was doing previously, but without the autorelease, and the release on the dealloc method as follow:
viewPtr = [[UIView alloc] initWithFrame:frmRect];
.
.
.
.
.
.
.
.
- (void)dealloc {
[viewPtr release];
[super dealloc];
}
The autorelease is the main problem in your code, because when you do this, your UIView won't respond to any event.
Cheers,
VFN
What is self in [btnPointer addTarget:self ... ? Is generate: called? Have you tried debugging there?
As vfn says, this you get the error because you release the view too early. The question I would be asking is why is this case different?
Normally in an assignment like this:
viewController.view = viewPtr;
The viewPtr is retained and you would be correct to release it yourself. But look at the definition:
#property(nonatomic, retain) UIView *view
This means that any value is simply assigned and not automatically retained.