Unable to set custom class for UICollectionView in Storyboard - ios

I'm unable to set the custom class attribute in Storyboard mode for a UICollectionView. Below you can see the screenshot of where I'm entering this. Generally when I extend a view controller it will autocomplete the custom class name, but in this case it doesn't, and if I click away to a different UI element and click back to the CollectionView then the custom class is empty again (shows the UICollectionView placeholder).
Here is the MyMediaCollectionController.m
#import "MyMediaCollectionController.h"
#interface MyMediaCollectionController ()
#end
#implementation MyMediaCollectionController
static NSString * const reuseIdentifier = #"Cell";
- (void)viewDidLoad {
[super viewDidLoad];
[self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
#end
and MyMediaCollectionController.h:
#import <UIKit/UIKit.h>
#interface MyMediaCollectionController : UICollectionViewController
#end
Thanks in advance!

From your code and screenshot it seems that you subclassed the wrong base class. You need to use UICollectionView instead of UICollectionViewController
Change:
#interface MyMediaCollectionController : UICollectionViewController
#end
to:
#interface MyMediaCollectionController : UICollectionView
#end
When you do this you need to change your implementation also, change it like:
#implementation MyMediaCollectionController
static NSString * const reuseIdentifier = #"Cell";
- (void)awakeFromNib
{
[super awakeFromNib];
[self registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
}
#end

Your class must be subclass of UIViewController
MyMediaCollectionController.h:
#import <UIKit/UIKit.h>
#interface MyMediaCollectionController : UIViewController <UICollectionViewDelegate>
#end

Related

Warning "Method definition not found" in Objective-C. Call Method from Category file

I have created some method in category file. I just want to reuse those methods in my view controllers. So I have imported that category file in view controllers and declared the method in header file also.
calling it like this:
Category class:
#interface UIViewController (headerView)
-(UILabel *)someMethod;
#implementation UIViewController (headerView)
-(UILabel *)someMethod{
}
HomeViewController:
#interface HomeViewController : UIViewController
-(UILabel *)someMethod;
#implementation HomeViewController
[self someMethod];
I am getting warning message in this line:
#implementation HomeViewController
It's working. But I want to clear this warning. How can I do it?
If you want you category in view controller do it something like that
Your category
#interface UIViewController (ExtendedMethods)
- (void)someMethod;
#end
#implementation UIViewController (ExtendedMethods)
- (void)someMethod {
NSLog(#"Some method");
}
#end
MyViewController.m
#import "MyViewController.h"
#import "UIViewController+ExtendedMethods.h"
#implementation MyViewController
- (void)viewDidLoad {
[super viewDidLoad];
[self someMethod];
}
#end

Xcode - How to call method from another class

my first class .h :
#interface CollectionViewController : UICollectionViewController <..>
#property (strong, nonatomic) IBOutlet UICollectionView *collectionView;
-(void)refreshView;
#end
my first class .m
#implementation CollectionViewController
-(void)refreshView
{
[self.collectionView reloadData];
}
#end
i need to call this method from my second class
my second class .h
#interface CollectionViewCell : UICollectionViewCell
- (IBAction)buttonAction:(id)sender;
#end
my second class .m
#implementation CollectionViewCell
- (IBAction)myButtonAction:(id)sender {
//i need to call the method "refreshView" here
}
#end
I tried to edit the method refreshView from private to public but it's not compatible with "[self.collectionView reloadData];"
Thanks for help.
It's not a good idea to have your cell "know about" its collection view.
Use delegation instead...
CollectionViewCell.h
#protocol CollectionViewCellDelegate <NSObject>
- (void)refreshButtonPressedForCell:(CollectionViewCell *)cell;
#end
#interface CollectionViewCell : UICollectionViewCell
#property (nonatomic, weak) id<CollectionViewCellDelegate> delegate;
#end
CollectionViewController.m
#interface CollectionViewController : UICollectionViewController <CollectionViewCellDelegate>
#end
#implementation CollectionViewController
- (void)refreshButtonPressedForCell:(CollectionViewCell *)cell {
[self.collectionView reloadData];
}
#end
Don't forget to set the delegate property on each of your cells!

iOS Protocol Issues

I have been staring at code too long and know i am doing something silly here with my Protocols if someone could enlighten me that would be great.
Trying to get my areaNameLabel to change to cell.nameLabel.text across viewcontrollers.
FirstTableViewController.h
#import <UIKit/UIKit.h>
#import "FirstTableCell.h"
#import "SecondViewController.h"
#interface FirstTableViewController : UITableViewController <UITableViewDataSource, UITableViewDelegate, passNames>
#property (nonatomic, strong) NSString *passedNameString;
#property (strong, nonatomic) NSMutableArray *names;
FirstTableViewController.m
#import "FirstTableViewController.h"
#interface FirstTableViewController ()
#end
#implementation FirstTableViewController
#synthesize names;
#synthesize passedNameString;
- (void)viewDidLoad
{
[super viewDidLoad];
names = [NSMutableArray arrayWithObjects:#"Bondi", #"Miranda", nil];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
FirstTableCell *cell = (FirstTableCell *)[tableView cellForRowAtIndexPath:indexPath];
if ([cell.nameLabel.text isEqualToString:#"Bondi"]) {
SecondViewController *mapController = [[SecondViewController alloc] init];
NSString *passedName = cell.nameLabel.text;
mapController.passedNameString = passedName;
[mapController setDelegate:self];
self.tabBarController.selectedIndex = 1;
NSLog(#"Hola");
}
[tableView deselectRowAtIndexPath:indexPath animated:YES];
}
#pragma mark - Protocol Methods
-(void)setAreaName:(NSString *)areaName {
passedNameString = areaName;
}
SecondViewController.h
#import <UIKit/UIKit.h>
#protocol passNames <NSObject>
-(void)setAreaName:(NSString *)areaName;
#end
#interface SecondViewController : UIViewController <RMMapViewDelegate>
#property (retain) id <passNames> delegate;
#property (nonatomic, strong) NSString *passedNameString;
#property (weak, nonatomic) IBOutlet RMMapView *mapView;
#property (weak, nonatomic) IBOutlet UILabel *areaNameLabel;
#end
SecondViewController.m
#import "SecondViewController.h"
#import "FirstTableViewController.h"
#interface SecondViewController ()
#end
#implementation SecondViewController #synthesize areaNameLabel; #synthesize delegate, passedNameString;
- (void)viewDidLoad {
[super viewDidLoad];
passedNameString = areaNameLabel.text;
[[self delegate] setAreaName:passedNameString];
if ([areaNameLabel.text isEqualToString:#"Bondi"]) {
NSLog(#"You got it!");
}
}
Any other critiques feel free to throw in - I've had a look at some other Protocol questions and examples but i know it is something obvious i am missing.
The problem is that your SecondViewController has no relationship to the passNames protocol (being declared in the same header does not count).
Since protocol methods need to be implemented (or their implementation be inherited from the base) and your SecondViewController does not do that, you cannot call setAreaName: without triggering an error.
If you would like to use a common protocol in two view controllers, you need to do this:
Give passNames protocol a more conventional name that starts in a capital letter, and put it in a separate header file
Include that header in both view controllers (the #import "SecondViewController.h" in the FirstTableViewController.h does not look right)
Put implementations of setAreaName: in both view controllers.
Note that you cannot put the common functionality in a superclass, because your view controllers inherit from different bases (i.e. UIViewController and UITableViewController).

iOS accessing NSObject class from my viewController

I'm trying to access to another class from my viewcontoller but is not working:
viewcontroller.h
#import <UIKit/UIKit.h>
#class firstClass; //nsobject class
#interface ViewController : UIViewController
{
firstClass *firstclass;
}
#property (retain,nonatomic) LEMZfirstClass *firstclass;
---
firstClass.h:
#import "LEMZViewController.h"
#interface firstClass : NSObject
{
ViewController *viewController;
}
#property (retain,nonatomic) ViewController *viewController;
-(void)doSomenthing;
firstClass.m:
#synthesize viewController;
-(void)doSomenthing
{
viewController.firstclass=self;
viewController.outPutLabel.text=#"This is my Label";
}
viewcontroller.m:
#synthesize firstclass;
- (void)viewDidLoad
{
[super viewDidLoad];
[firstclass doSomenthing];
}
it compiles with no errors but the label is never updated and for that matter the first class is never call it all. What I'm doing wrong? I'll really appreciate your help.
A few things I'm noticing:
Generally you would have the ViewController class handle updating its own UI elements, not another class.
Where is your outPutLabel variable? Is it created by code or an IBOutlet that is wired up in InterfaceBuilder?
Before you can call something on firstclass, you must create it. Something like this:
firstclass = [[firstClass alloc] init];
[firstclass doSomenthing];
The viewController.firstclass=self; line would be redundant then.
Your firstClass.h
#import <Foundation/Foundation.h>
#interface firstClass : NSObject
+(NSString *)doSomenthing; //Instance Class
#end
firstClass.m
#import "firstClass.h"
#implementation firstClass
+(NSString *)doSomenthing
{
return #"This is my Label";
}
#end
ViewController.h
#import <UIKit/UIKit.h>
#import "firstClass.h"
#interface ViewController : UIViewController
#end
ViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[firstClass doSomenthing];
outPutLabel.text=[firstClass doSomenthing];;
// Do any additional setup after loading the view, typically from a nib.
}
Note: Here I am using instance class. Before you work this code you must study about Instance class.

How get a custom UITableViewCell to appear in a UITableView in custom UIViewController with storyboard?

I think I"m really close now, but I'm missing something. If I break before I return the cell in the tableView:cellForRowAtIndexPath method, the cell is holding the correct data. But it doesn't appear in the table. I'm assuming that my UITableView is not hooked up to the UIViewController properly.
Here is my custom cell: SCCustomerTableCell.h
#import <UIKit/UIKit.h>
#import "SCCustomer.h"
#interface SCCustomerTableCell : UITableViewCell
#property (strong, nonatomic) IBOutlet UILabel *companyLabel;
-(void)configureCellWithCustomer:(SCCustomer *)customer;
#end
SCCustomerTableCell.m
#import "SCCustomerTableCell.h"
#implementation SCCustomerTableCell
-(void)configureCellWithCustomer:(SCCustomer *)customer
{
self.companyLabel = [[UILabel alloc] init];
self.companyLabel.text = [customer dbaName];
}
#end
And the custom view controller: SCCustomersVC.h
#import <UIKit/UIKit.h>
#import "SCCustomerTableCell.h"
#import "SCDataObject.h"
#interface SCCustomersVC : UIViewController <UITableViewDelegate, UITableViewDataSource, UISearchBarDelegate>
#property (strong, nonatomic) IBOutlet UITableView *customersTable;
#property SCDataObject *dataObject;
#property (nonatomic, strong) NSArray *customers;
#end
SCCustomersVC.m
#import "SCCustomersVC.h"
#import "SCSplitVC.h"
#import "SCDataObject.h"
#interface SCCustomersVC ()
#end
#implementation SCCustomersVC
#synthesize customersTable;
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [self.customers count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomerCell";
[self.customersTable registerClass:[SCCustomerTableCell class] forCellReuseIdentifier:CellIdentifier];
SCCustomerTableCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
SCCustomer *customer = (SCCustomer *)[self.customers objectAtIndex:indexPath.row];
[cell configureCellWithCustomer:customer];
return cell;
}
Edit: I forgot screens of storyboard connections:
SCCustomersVC, UITableView, SCCustomerTableCell, UILabel
Ok, there are several things you're doing wrong. I see from your screen shots that you didn't set your controller as the data source or delegate of the table view. You need to do that. Second, if you make the custom cell in the table view in IB, you don't need to register the class, just make sure that you have the identifier set. Third, since you made the label in IB, and you have an outlet to it, you don't need to alloc init it in your cell's .m file -- in fact you don't need anything at all in the .m . Just put cell.companyLabel.text =... in the cellForRowAtIndexPath method.

Resources