App crashes when adding UICollectionView within a Nib file - ios

I'm building an experimentation app using the HMSegmentedControl framework. On this app I'm trying to add a UICollection view into a view made on a xib file, but I'm having trouble with it. The app crashes and I get this:
2016-02-01 10:23:40.693 SegmentControlExperimentation[1328:49671] *** Assertion failure in -[UICollectionView _dequeueReusableViewOfKind:withIdentifier:forIndexPath:viewCategory:], /BuildRoot/Library/Caches/com.apple.xbs/Sources/UIKit_Sim/UIKit-3512.30.14/UICollectionView.m:3690
2016-02-01 10:23:40.699 SegmentControlExperimentation[1328:49671] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MyCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
I've been looking it up everywhere, but I couldn't come with a solution.
So I made a github repo and I cloned my code on this link: https://github.com/nascimentorafael/SegmentControlExperimentation
It is a really small and simple code. If one runs it, will get this crash. So have a look on the ViewController class and the view.xib.
I hope someone can help me with this.

I browsed your github repository and found you didn't practice UICollectionView in a right way. However, if you would like to avoid the crash only, just comment out codes line 102-105 & line 128 in your ViewController.m
If you would like to put have your collectionView in view.xib, you need to insert this line in view.h
#property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
Refers above outlet in view.xib and comment out line 35-37 in ViewController.m
And then add the following codes for View *view in - (void)viewDidLoad {}
UINib *cellNib = [UINib nibWithNibName:#"CustomCollectionViewCell" bundle:nil];
[view.collectionView registerNib:cellNib forCellWithReuseIdentifier:#"MyCell"];
view.collectionView.delegate = self;
view.collectionView.dataSource = self;
Still please understand that the above recommendations are minimum changes for your current repository and may not be the best practice. There are many things need to be put into consideration and the combination will be very different according to each scenario.

Related

BAD_ACCESS & NSInvalidArgumentException in working with UIPickerView

I'm beginner in working with UIKit. When I tired to work with UIPickerView in iOS 7 SDK I faced to a hard-to-solve problem.
We know that UIPickerView needs two resources to work perfectly: dataSource & delegate. So I wrote a class named "KMPickerProtocols". I adopted it to UIPickerViewDataSource & UIPickerViewDelegate Protocols and then I added some extra setter methods to it.
KMPickerProtocols is adopted to all of essential methods of dataSource and delegate Protocols + the necessary optional method for setting title of each row (pickerView:titleForRow:forComponent). all these methods get things done without any problem (in normal situations).
Finally I set the delegate and dataSource properties of my UIPickerView (named _accountPicker) manually with this piece of code:
NSArray *delegateAgent = [[KMTwitterDelegate new] run ];
_accountPicker.dataSource = [delegateAgent objectAtIndex:0];
_accountPicker.delegate = [delegateAgent objectAtIndex:1] ;
(the run method is designed to set some properties including height of each row and ...)
Now, when I run my app it will show my twitterAccounts stored in system(accounts framework) in the form of UIPickerView. but there is a problem: As soon as I scroll the picker view or tap on any row, the program will crash and I get BAD_ACCESS (code=2 , address = 0x1) in this line of code:
#autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([KMAppDelegate class]));
}
and also there are sometimes that I get signal SIGABRT in above line. in these situations Log Says:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSMallocBlock pickerView:titleForRow:forComponent:]: unrecognized selector sent to instance 0x8c75ad0'
I will appreciate if you help me to deal with this problem. I really don't know where the bug is. but I guess this screen shots will be helpful to find that:
The problem in above screenshot is that UIPicker called the "pickerView:titleForRow:forComponent" method for row 0, three times instead of one time. I just don't know whether that is this because myFault or because the typical behaviour of UIPicker.
You are sending the message pickerView:titleForRow:forComponent: to an object of type NSMallocBlock. This is a message automatically sent from the picker to its datasource, so my assumption is that you are assigning the datasource:
_accountPicker.dataSource = [delegateAgent objectAtIndex:0];
The datasource you assign does not conform the protocol UIPickerViewDataSource
Checkout what returns [delegateAgent objectAtIndex:0]; and make sure it is what you expect it to be.

Unable to control UICollectionView using property

SO, I have a property to a UICollectionView Object
#property (strong, nonatomic) IBOutlet UICollectionView *collectionViewVARIABLE;
I held control and dragged the mouse, it's all linked up.
my problem is that it doesn't seem to actually work.
I try calling
[self.collectionViewVARIABLE registerClass:[MFH_mainfeed_CondensedViewCell class] forCellWithReuseIdentifier:#"MainIpadFeedCell"];
I get this error:
Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'could not dequeue a view of kind: UICollectionElementKindCell with identifier MainIpadFeedCell - must register a nib or a class for the identifier or connect a prototype cell in a storyboard'
If I put this call within cellForItemAtIndexPath using collectionView instead of the variable, it works.
I've tried the following on a button click, also nothing.
self.collectionViewVARIABLE.backgroundColor = [UIColor blackColor];
I have been at this for three days. My head hurts. Can somebody please tell me what I am missing.

iOS load from xib not works

I have created a Custom View Controller with XIB file.
In XIB file, I put a tableview and a slider to the view and connect all of them as IBOutlet to the File's Owner.
I check the File's owner class and it is my custom class, so it's ok.
I double checked the view and slider are connected with File's Owner.
But when I try to load my custom viewcontroller, I got this error:
*** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<NoteController 0x6bd1da0> setValue:forUndefinedKey:]: this class is not key value coding-compliant for the key _slider.'
Slider is my IBOutlet (which is connected to the view controller)
I found a lot of same problems on the internet, but all solutions not works for me :(
The load code:
NSArray *xibViews=[[NSBundle mainBundle]loadNibNamed:#"PopoverSetFont" owner:self options:nil];
What should I try?
After 2 hours, and when I ask this question, found a problem. I do not delete a question, I share it, I hope it helps.
So instead of the [[NSBundle mainBundle]... line, use this for load:
PopoverSetFont *popoverView=[[PopoverSetFont alloc]initWithNibName:#"PopoverSetFont" bundle:nil];

Popovers in UITableView

I would like to display popovers with UITableView's as content (this works) on some button presses and then get the selected item as string as buttontitle or some textview text. I've found a few example on how to do this with protocols but still get an error.
My code:
In popoverViewController.h
#protocol popoverViewControllerDelegate <NSObject>
-(void)getRowText:(NSString *)string;
#end
I declare an id delegate2 variable and set its property to:
#property(nonatomic,assign) id<popoverViewControllerDelegate> delegate2;
In the popoverViewController.m file I synthesize the variable, and in didSelectRowAtIndexPath method I have this, and this line seems to cause the error I`m having:
[self.delegate2 getRowText:[someArray objectAtIndex:indexPath.row];
In mainViewController.m I add the popoverViewControllerDelegate to the ViewControllers protocol and have its header file imported. And then have some code in the -(void)getRowText: method which doesnt get called.
UIPopovers and such are set up as they work as needed, problem arises when I press a row in the tableview. I get the
Terminating app due to uncaught
exception
'NSInvalidArgumentException', reason:
'* -[UIPopoverViewController
getRowText:]: unrecognized selector
sent to instance 0x57ca80'
Could anyone give some advice with this?
Finally found the error and cant believe how silly I/it was.
I had a viewController.delegate2 = self. line with period instead of a semicolon, I wonder why it compiled tho.

Set Delegate for Subview Controller

i'm sure there is a simple way to solve this problem, but I can't figure out.
I got a TabBarViewController with several Views. In some of the Views i loaded Tableviews with XML based content.
My XML Parser works fine, I tested it before in a separate Projectfile.
The Problem is that my TableViewController, for which I created a XMLAppDelegate to manage the parsing of my xmlfiles, still sends the methods to the main delegation file of my Project.
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MainAppDelegate slices]: unrecognized selector sent to instance 0x6d1fd00'
Like the error message shows, he tries to send it to my MainAppDelegate, instead of the XMLAppDelegate.
I implemented the XMLAppDelegate in my Controller Files and init the delegate this way:
The .h File of my Controller
#import <UIKit/UIKit.h>
#class XMLAppDelegate, NewsDetailViewController;
#interface NewsViewController : UITableViewController <UITableViewDelegate, UITableViewDataSource> {
IBOutlet UITableView *newsTableView;
XMLAppDelegate *appDelegate;
NewsDetailViewController *ndvController;
}
#end
The .m File of my Controller
- (void)viewDidLoad {
[super viewDidLoad];
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
appDelegate = (XMLAppDelegate *)[[UIApplication sharedApplication] delegate];
self.title = #"Slices";
}
The Controller is loading into the TabBarView as separate .xib file.
In this nib file i connected the Delegate and Data Source to my XMLAppDelegate.
I still get this Error in the Log and the Application crashes as I try to switch to the Tab wish the TableViewController in it.
Hope you understand my problem. Maybe someone could tell me what I'm doing wrong here setting the Delegate to my XMLAppDelegate.
EDIT:
To explain what I want to do:
I have a XMLParser class to parse the File, a Slice class to store the Elements and XMLAppDelegate. From this files i get my XML results stored in an Array with name "slices".
To use the Elements of the Array, for showing it up as Table or textlabel, I connected the TableViewController to the XMLAppDelegate (which parses my XML Document using the XMLParser File and stores Data in the Array).
The Items of this Array are previously declared in the Files as Strings like this
#property (nonatomic, readwrite) NSInteger sliceID;
#property (nonatomic, retain) NSString *image;
#property (nonatomic, retain) NSString *title;
#property (nonatomic, retain) NSString *text;
But like i said, the parser works.
I think my problem seems to be that I requested appDelegate.slices, but the selector is not my XMLAppDelgate. The Error says it uses the Main Delegate, which don't have any propertys of the xml Delegate file.
The TabView loads the NewsViewController, which calls for the array in the XMLAppDelegate.
I implemented the files, connected the Delegate to TableView as Data Source and Delegate, but still it seems to take the main delegate.
That drives me crazy.
The Project is a Company Profile for a great Wellness Motel.
I get the Contents from the CMS as XML-Files and need to access them from XMLAppDelegate, that delivers the sites contents.
Hope you understand my bad English. ;)
So, now you've declared the property, but you haven't implemented it. A property declaration, among other things, declares that you will implement one or two accessor methods for the property. So, it's not (yet) enough just to declare the property; you must also implement the accessor methods that make that property real at run time.
The easiest way to fix this is to synthesize the accessors for the slices property. If you need some custom behavior, you can implement them yourself, but it doesn't sound like it—synthesizing is not only easiest, but probably the right solution.
See the documentation on properties for more information.
Maybe I have no idea how about your XMLappDelegate, but in my opinion
[[UIApplication sharedApplication] delegate]
always gives you the main AppDelegate, whatever you are casting it into.
Maybe you should add a reference to your XMLAppDelegate to your Main App Delegate and declare it as a property, then you could access the XMLAppDelegate by
AppDelegate.xmlDelegate
Then again I don't really understand what you are trying to do..
Maybe it helos anyway ;)

Resources