Access UISwitch in Prototype Cell - ios

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

Related

UIButton addTarget with parameters inside UIViewCollectionView

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.

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!

Sending a info to detailview from UICollectionView is not working?

I think i didnt explain well in the title, heres the problem, I have a collection view that is supplying from a json, but when I go to the detail it crashes, i think the problem is in this part of the code when I send the info to the detail:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
if ([[segue identifier]isEqualToString:#"detailView"]) {
NSIndexPath *myIndexPath = [self.collectionView indexPathsForSelectedItems];
int row = [myIndexPath row];
MenuDetailViewController *menu = [segue destinationViewController];
EntryJson *entry = [[InfoWeb sharedInstance] entryAtIndex:row];
menu.galeriademo = entry.imagenes;
menu.DetailModalprim = #[entry.title == nil ? #"" : entry.title,
entry.date == nil ? #"" : entry.date,
entry.desc == nil ? #"" : entry.desc,
entry.mainimage == nil ? #"" : entry.mainimage,
entry.seccion == nil ? #"" : entry.seccion];
}
}
In a tableview the IndexPath would be like: NSIndexPath *myIndexPath = [self.collectionView indexpathforselectedRow];
I had a tableview working like this and it was fine but when I change to a collection view i had this problem. The crash log is the next:
2014-03-07 12:41:00.696 CMT[28640:70b] -[__NSArrayI row]: unrecognized selector sent to instance 0xc34d180
2014-03-07 12:41:00.698 CMT[28640:70b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI row]: unrecognized selector sent to instance 0xc34d180'
So I need your help! Thanks!
indexPathsForSelectedItems returns an NSarray, not an NSIndexPath
NSIndexPath *myIndexPath = [self.collectionView indexPathsForSelectedItems];
should be
NSArray *myIndexPaths = [self.collectionView indexPathsForSelectedItems];
NSIndexPath *myIndexPath = [myIndexPahts objectAtIndex:0];

Add row to UITableViewController from child

I have UIViewController that performs a login check with the code below, and a UITableViewController as its parent controller. I'm trying to add another row to the UITableViewController after switching back to it, however I'm getting the following error:
[UINavigationController insertNewObject:]: unrecognized selector sent to instance 0x14de164d0
2014-03-08 17:00:27.379 MyApp[2562:60b] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController insertNewObject:]: unrecognized selector sent to instance 0x14de164d0'
-
if(loginSuccess)
{
[self.navigationController popViewControllerAnimated:YES];
[self.navigationController performSelector:#selector(insertNewObject:) withObject:#"Another Row"];
}
The method definition of insertNewObject is:
- (void)insertNewObject:(NSString *)name
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:name atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:#[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
}
Can someone help me with what I'm doing wrong?
You're calling insertNewObject on a UINavigationController not your UITableViewController.
Try creating a property on your UIViewController that points to the original TableViewController and calling it through the property. Or calling it when the TableViewController reappears.

How to jump to the next row in detailview

i have a TableView which loads a detailview in didSelectRow. I have no problems with the data's.
I am using coreData and to populate the TableView i am using a NSFetchedResultsController.
Now, i want to make a next and previous function in the detailView to jump to the next or previous Table(row) Element. Next Previous like in the Mail-App.
I know that i have to use a button with IBAction.
But how can i make this function?
thanks,
brush51
Edit 1:
I have it done so:
- (IBAction) previousCard:(id) sender {
NSLog(#"previousBtn");
DashboardViewController *parent = (DashboardViewController *)self.parentViewController;
NSIndexPath *actualIndexPath = selectedRow2;
NSIndexPath * newIndexPath = [NSIndexPath indexPathForRow:actualIndexPath.row-1 inSection:actualIndexPath.section];
NSLog(#"newIndexPath: %#", newIndexPath);
[parent.tableViews selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle]; //HERE I GET SIGABRT
}
But i get an error:
-[UIViewController tableViews]: unrecognized selector sent to instance 0xd758ae0
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIViewController tableViews]: unrecognized selector sent to instance 0xd758ae0'
How to solve this?
With your UITableView, you can get the selected row by:
- (NSIndexPath *)indexPathForSelectedRow
And set the selection by incrementing the indexPath.row:
- (void)selectRowAtIndexPath:(NSIndexPath *)indexPath animated:(BOOL)animated scrollPosition:(UITableViewScrollPosition)scrollPosition
then you action could be:
-(IBAction)nextCell:(id)sender {
NSIndexPath * actualIndexPath = myTableView.indexPathForSelectedRow;
NSIndexPath * newIndexPath = [NSIndexPath indexPathForRow:actualIndexPath.row+1 inSection:actualIndexPath.section];
[myTableView selectRowAtIndexPath:newIndexPath animated:YES scrollPosition:UITableViewScrollPositionMiddle];
}
for that you can manage with your DataSource array.
when you load your detail view at that time you pass your index of array(indexPath.row). by that index you fetch data in detail view. and using increment and decrement in that index you get data for next and previous cell.
Steps
1) setup your tableView.
2) when redirect to DetailView at that time set one int variable for store current indexPath.row . so define one int in detail View.
3) using next and previous button manage indexpath.row which is in int variable.
and represent appropriate data from Source array.
You can use:
(UITableViewCell *)cellForRowAtIndexPath:(NSIndexPath *)indexPath
To get the cell you want (next or previous).
And then set the returned cell "Selected" property to YES.
cell.selected = YES

Resources