UIButton addTarget with parameters inside UIViewCollectionView - ios

I'm getting the following error when I addTarget to UIButton inside UiCollectionView.
My code:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIButton *btnBonus = (UIButton *) [cell viewWithTag:405];
[btnBonus setTag: [arrayTruebonusTags[0] intValue]];
[btnBonus addTarget:self action:#selector(goBonus:) forControlEvents:UIControlEventTouchUpInside];
return cell;
}
- (void) goBonus:(id) sender
{
UIButton *button = (UIButton *) sender;
}
And I get this error:
[Controller goBonus]: unrecognized selector sent to instance 0x16dc1190
2014-11-08 11:11:41.991 demo[3570:1707966] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Controller goBonus]: unrecognized selector sent to instance 0x16dc1190'
*** First throw call stack:
(0x299cdc1f 0x375b2c8b 0x299d3039 0x299d0f57 0x29902df8 0x2cebdc2b 0x2cebdbd1 0x2cea8863 0x2cebd63d 0x2ce8242d 0x2ceb72f1 0x2ceb6bcd 0x2ce8d3dd 0x2d100c29 0x2ce8be39 0x29994377 0x29993787 0x29991ded 0x298e0211 0x298e0023 0x30cbf0a9 0x2ceec1d1 0xf3599 0x37b32aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
The problem is that if I do the same without goBonus: and in the method -goBonus{} it works like a charm.

The crash log you posted complains about a missing method [Controller goBonus].
The code you posted shows you adding the action goBonus: (with a colon, meaning it takes a parameter).
The fact that the crash doesn't match your code tells me that you have a mismatch somewhere. The selector in your addTarget method, #selector(goBonus:), is correct for the method you posted, but the crash log is complaining about a missing selector #selector(goBonus) (no colon, hence no parameter.)
You need to sort that out.

Related

CollectionView NSInvalidArgument when setting image in cell's ImageView

I'm working on a collection view to prompt images and I run into an issue. When I try to set an image to my UIImageView (in my cell) It crashes saying NSInvalidArgument here is the log:
-[UICollectionViewCell img]: unrecognized selector sent to instance 0x7ff063f81170
2018-07-10 10:10:50.645635+0200 App[41377:5706851]
*** Terminating app due to uncaught exception 'NSInvalidArgumentException',
reason: '-[UICollectionViewCell img]:
unrecognized selector sent to instance 0x7ff063f81170'
Here is the part of the code the error:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
cell.img.image = [UIImage imageNamed:#"test"];
return cell;
}
I'm a 100% sure that it is supposed to work because I used the same method for a UITableView and the images were displayed properly.
Thanks for your reply.
UPDATE:
here is a screen of my storyboard if it can help:
According to the error you posted:
-[UICollectionViewCell img]: unrecognized selector sent to instance 0x7ff063f81170
The reason is obvious that the cell instance dequeued from tableview doesn't have the method "img".
I guess maybe the reuseIdentifier is incorrect so that you got wrong class cell instance which does not implement "img" method.
check your reuse identifier and cast your cell to your class
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath {
CollectionViewCell *cell = (*CollectionViewCell)[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
// Configure the cell
cell.img.image = [UIImage imageNamed:#"test"];
return cell;
}

Overriding didSelectRowAtIndexPath on a framework-provided view controller?

So I'm using the CoreAudioKit's CABTMIDICentralViewController to present a list of MIDI Bluetooth devices for the user to select. However, I want to be able to tell which device the user selected when they're done, but it doesn't seem Apple has added any way to do that.
So I'm trying to hack it by detecting when the user selects a row in the table:
DPBleMidiDeviceManager.h:
#import <CoreAudioKit/CoreAudioKit.h>
#interface DPBleMidiDeviceManager : CABTMIDICentralViewController
#end
DPBleMidiDeviceManager.m:
#import "DPBleMidiDeviceManager.h"
#implementation DPBleMidiDeviceManager
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"midi device selected %#", indexPath);
//either of these next lines crash, it makes no difference:
//[[tableView delegate] tableView:tableView didDeselectRowAtIndexPath:indexPath];
[super tableView:tableView didDeselectRowAtIndexPath:indexPath];
}
#end
The problem is, it's crashing on the last line saying there's no selector. Which is weird, because if I remove the super call, it doesn't crash, but it also doesn't correctly connect to the BLE device, like it would if I was not overriding that delegate call.
Is this just something Apple does so you can't access their tables? Why would they build a UI view like this and let you call it, but not give you any information about what the result was? Am I missing some standard way to do this?
EDIT: here's the details of the crash with the super call:
2015-10-29 15:14:37.039 [626:338267] midi device selected <NSIndexPath: 0x1473ae20> {length = 2, path = 0 - 3}
2015-10-29 15:14:37.039 [626:338267] -[DPBleMidiDeviceManager tableView:didDeselectRowAtIndexPath:]: unrecognized selector sent to instance 0x147f34e0
2015-10-29 15:14:37.040 [626:338267] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[DPBleMidiDeviceManager tableView:didDeselectRowAtIndexPath:]: unrecognized selector sent to instance 0x147f34e0'
*** First throw call stack:
(0x2b3cdfef 0x3967dc8b 0x2b3d3409 0x2b3d11bf 0x2b300e78 0xaa165 0x2eb3956b 0x2ebe843b 0x2ea9da91 0x2ea1838f 0x2b393fed 0x2b3916ab 0x2b391ab3 0x2b2de201 0x2b2de013 0x32aab201 0x2ea82a59 0x88447 0x39c09aaf)
libc++abi.dylib: terminating with uncaught exception of type NSException
That's not how you call super. It should be
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSLog(#"midi device selected %#", indexPath);
[super tableView:tableView didDeselectRowAtIndexPath:indexPath];
}

Can't find method with #selector - NSInvalidArgumentException since iOS 8

after updating to iOS8 and compiling my app with XCode6, I get a very strange exception when clicking on a button.
My button is defined like this:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self
action:#selector(cellButtonPressed:)
forControlEvents:UIControlEventTouchDown];
In "#selector" I defined the method which is called when button is pressed:
-(void) cellButtonPressed:(id)sender {
NSLog(#"Hello again");
}
I also added this method to my header file .h
This button is placed into a UITableViewCell as a subview:
button.frame = CGRectMake(cell.frame.size.width - 54, cell.frame.origin.y+20, 36, 36);
[cell addSubview:button];
This worked very fine on iOS7. But now, on iOS8, I get an exception after clicking on the button:
2014-09-25 08:33:47.461 ****[12442:1669165] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewWrapperView type]: unrecognized selector sent to instance 0x12ce35560'
*** First throw call stack:
(0x185dae084 0x1963940e4 0x185db5094 0x185db1e48 0x185cb708c 0x10005a928 0x18a5652f4 0x18a54e44c 0x18a56dff8 0x18a524724 0x18a55e7b8 0x18a55de58 0x18a531660 0x18a7cfd6c 0x18a52fbc8 0x185d66324 0x185d655c8 0x185d63678 0x185c91664 0x18edd35a4 0x18a596984 0x10004b324 0x196a02a08)
libc++abi.dylib: terminating with uncaught exception of type NSException
Does anybody know why?
Thx for you help!
Ok, I found the answer because of an advice in comments:
The method in selector is called properly. Problem exists in getting cell from superview.
On iOS7 I get the clicked button within cell with following code:
UserTableViewCell *cell = (UserTableViewCell *)[[sender superview] superview];
Now, on iOS8, I have to get the cell with this call:
UserTableViewCell *cell = (UserTableViewCell *)[sender superview];
So, this is a solution for me:
-(void)cellButtonPressed:(id)sender {
NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:#"."];
UserTableViewCell *cell = nil;
if ([[vComp objectAtIndex:0] intValue] >= 8) {
cell = (UserTableViewCell *)[sender superview];
} else {
cell = (UserTableViewCell *)[[sender superview] superview];
}
// do your stuff
}
So, view stack of TableViewCell seems to be another. Good to know :)
Thanks guys!

Access UISwitch in Prototype Cell

I'm trying to print on/off when the user changes the state of my UISwitch.
For example
- (IBAction)toggleSwitch:(id)sender {
ChannelsTableViewCell* cell = (ChannelsTableViewCell *)[sender superview].superview;
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
if (cell.localSwitch.on)
{
NSLog(#"On");
}
}
When I run this it throws an error.
2014-04-24 11:33:41.462 HRApp[3258:60b] -[UITableViewCellScrollView localTitle]: unrecognized selector sent to instance 0x19354410
2014-04-24 11:33:41.467 HRApp[3258:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UITableViewCellScrollView localTitle]: unrecognized selector sent to instance 0x19354410'
If you hook up the IBAction to the switch, you don't need to get the UITableViewCell to check for the switch's value. You can use the sender parameter from the IBAction. Hence:
- (IBAction)toggleSwitch:(id)sender {
UISwitch *switch = (UISwitch *)sender;
if (switch.on)
{
NSLog(#"On");
}
}
If you need to find the indexPath at which the UISwitch is shown, you can add the following:
CGPoint pointInTable = [switch convertPoint:switch.bounds.origin toView:self.tableView];
NSIndexPath *indexPath = [self.tableView indexPathForRowAtPoint:pointInTable];

TableView throws error when trying to implement dymanic cell height

I'm trying to customize the height of my tableView cells. I tried a few tutorials, but nothing seems to work. "Funny thing" is that the error message is always the same. Here is the code I'm using atm:
#define PADDING 10.0f
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *text = [self.tabelle objectAtIndex:indexPath.row];
CGSize textSize = [text sizeWithFont:[UIFont systemFontOfSize:14.0f] constrainedToSize:CGSizeMake(self.tableView.frame.size.width - PADDING * 3, 1000.0f)];
return textSize.height + PADDING * 3;
}
And this is the error message I get:
2012-05-09 20:01:55.553 APP[436:f803] -[APP sizeWithFont:constrainedToSize:]: unrecognized selector sent to instance 0x687f5f0
2012-05-09 20:01:55.555 APP[436:f803] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[APP sizeWithFont:constrainedToSize:]: unrecognized selector sent to instance 0x687f5f0'
*** First throw call stack:
(0x128e022 0x1846cd6 0x128fcbd 0x11f4ed0 0x11f4cb2 0x42c9 0x3ab688 0x3ad862 0x25b66d 0x25b167 0x2f4d 0x34e8 0x29638f 0x2965eb 0x2a6ff1 0x2a785f 0x2a79e1 0x3c55c2 0x20bd21 0x128fe42 0xea679 0xf4579 0x794f7 0x7b3f6 0x108160 0x1cbe84 0x1cc767 0x1db183 0x1dbc38 0x1cf634 0x2181ef5 0x1262195 0x11c6ff2 0x11c58da 0x11c4d84 0x11c4c9b 0x1cbc65 0x1cd626 0x24a5 0x1d35)
terminate called throwing an exception
My first thought was that it doesn't like me setting the font size of the labels in the
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
But that doesn't seem to be the case
The reason you're getting this error is cause this:
[self.tabelle objectAtIndex:indexPath.row];
Will return the custom object you have in tabelle, not an NSString, and you're calling an NSString method on it. Maybe you mean to use
[[self.tabelle objectAtIndex:indexPath.row] Name]
or
[[self.tabelle objectAtIndex:indexPath.row] m_yolo]
Depending on which text you want to base the size on.
EDITED TO USE THE CUSTOM OBJECTS IN TABELLE

Resources