I am struggling with an issue which I simply do not understand. Here is the following scenario:
I try to create a multi-view wizard in iOS. After much consideration I decided to go with an approach that may not be ideal but will help me keep the code clean. I don't want to chain ten view controllers and manage the manually.
The wizard has one view controller. At start it loads a certain amount of UIView's into the view and allows me to scroll left and right. Code below.
The UIViews are created in interface builder and I have one XIB file containing all the views.
Each UIView has more IBOutlets (for example a label) which are connected to the File Owner, in this case the wizard view controller.
This all works really well EXCEPT for the fact that the labels on the loaded UIViews don't update. Here is the problem:
I call the wizard and it loads all UIViews as well as Outlets right then
I skip through the wizard.
But then the problem occurs:
Example:
Step 2 has a button to call the address book (which works)
The Address book opens, returns an object (which works)
But my Label on UIView "Step 2" doesn't update when I assign a value.
Only after I remove this UIView from the Wizard and add it back, it works. It seems to me that the UIView loads correctly but any subsequent changes to their outlets don't work.
By the way, I verified that the label as well as the buttons and everything is connected probably. I just cannot update the label when its loaded as part of the XIB and the value changes after the initial load.
Code:
Wizard loading UIViews:
-(void)configureWizard
{
self.showsHorizontalScrollIndicator = NO;
self.showsVerticalScrollIndicator = NO;
self.scrollEnabled = NO;
self.contentSize = CGSizeMake(CGRectGetWidth(self.frame) * [wizardDelegate numberOfSteps], CGRectGetHeight(self.frame));
self.delegate = self;
}
-(void)loadWizard
{
[self configureWizard];
[self updateNavButtonsIfNecessary];
currentIndex = 0;
for (int i = 0; i<[wizardDelegate numberOfSteps]; i++)
{
[self loadWizardStepAtIndex:i];
}
}
- (void)loadWizardStepAtIndex:(int)index {
CGRect frame = self.frame;
frame.origin.x = CGRectGetWidth(frame) * index;
frame.origin.y = 0;
UIView *view = [wizardDelegate viewForStepAtIndex:index];
view.frame = frame;
[self addSubview:view];
}
Wizard Main View Controller.h (File Owner)
#property (strong, nonatomic) IBOutlet UIView *memberwizard_1;
#property (strong, nonatomic) IBOutlet UIView *memberwizard_2;
#property (strong, nonatomic) IBOutlet UIView *memberwizard_3;
#property (strong, nonatomic) IBOutlet UIView *memberwizard_4;
#property (strong, nonatomic) IBOutlet UIView *memberwizard_5;
#property (strong, nonatomic) IBOutlet UIView *memberwizard_6;
#property (strong, nonatomic) IBOutlet UIView *memberwizard_7;
#property (strong, nonatomic) IBOutlet UIButton *wizard2_lookupExistingContact;
#property (strong, nonatomic) IBOutlet UILabel *wizard2_nameLabel;
Wizard Main View Controller.m (File Owner)
-(UIView *)viewForStepAtIndex:(int)index
{
[[[NSBundle mainBundle]loadNibNamed:#"wizard_newMember" owner:self options:nil] lastObject];;
switch (index) {
case 0:
return self.memberwizard_1;
break;
case 1:
return self.memberwizard_2;
break;
case 2:
return self.memberwizard_3;
break;
case 3:
return self.memberwizard_4;
break;
case 4:
return self.memberwizard_5;
break;
case 5:
return self.memberwizard_6;
break;
case 6:
return self.memberwizard_7;
break;
default:
return self.memberwizard_1;
}
}
Return function of the address book
-(void)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker didSelectPerson:(ABRecordRef)person
{
[self displayPersonRecord:person];
[self dismissViewControllerAnimated:YES completion:nil];
}
Trying to update the label on the second UIView (this is where the issue occurs - it doesnt work).
-(void) displayPersonRecord:(ABRecordRef)person
{
NSString *firstName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonFirstNameProperty);
NSString *lastName = (__bridge_transfer NSString*)ABRecordCopyValue(person, kABPersonLastNameProperty);
NSLog(#"Name is %#", [NSString stringWithFormat:#"%# %#", firstName, lastName]);
self.wizard2_nameLabel.text = [NSString stringWithFormat:#"%# %#", firstName, lastName];
[self.wizardDelegate refresh:self.memberwizard_2]; //if called, the current view will be removed and re-added - then the label gets updated.
}
But as I mentioned if I reload the view then it works (see screenshot)
Reload method IMPORTANT: this is nasty code and not used in production, its just a test to see if a reload of the view helps.
-(void)refresh:(UIView *) oldView
{
[oldView removeFromSuperview];
CGRect frame = self.frame;
frame.origin.x = CGRectGetWidth(frame) * currentIndex;
frame.origin.y = 0;
//UIView *view = [wizardDelegate viewForStepAtIndex:currentIndex];
oldView.frame = frame;
[self addSubview:oldView];
}
If at this point I remove self.memberwizard_2 view from the wizard and add it back in everything works but it doesn't right away. Could that be related to way I initiate the UIViews? By the way the NSLog in the last paragraph works as well showing the name of the contact.
In case anyone knows an easier way to deal with wizards let me know :) PageViewControllers are painful because I need to hand-over one object from screen to screen but need to display different actions in each screen.
Thanks much and apologies for the code mess, I tried to make it as clear as possible but maybe it isn't.
Here's where I think it goes wrong. For each "WizardStep", you call viewForStepAtIndex which loads the NIB and returns the relevant view. You then add that as a subview. But when you process the next WizardStep, the NIB gets loaded again, which creates new instances of all its objects and reconnects all the Outlets and Actions to the newly loaded objects. So the view you have just added as a subview (and so is displayed) is no longer pointed to by your outlets. Thus when you update the label, it is not updating the label on screen, but a completely different label on a view that's not in the hierarchy at all.
So, I would revise your code so that the loadNibNamed: method gets run only once.
Related
I need to subclass a UITabBarController so that I can completely replace the UITabBar view with a custom view that I can hopefully produce in the interface builder. I tried but am not succeeding.
First, I created a subclass of UITabBarController along with a xib. I deleted the default view in the xib, and replaced it with a new one that was only 60px tall (the size of my tabbar). I dragged the necessary buttons onto it, and configured the .h file like so:
#interface ToolbarViewController : UITabBarController
#property (strong, nonatomic) IBOutlet UIView *tabBarView;
#property (strong, nonatomic) IBOutlet UIButton* firstButton;
#property (strong, nonatomic) IBOutlet UIButton* secondButton;
#end
My xib looks like this:
When I launch the app, I see an empty space at the bottom made for the tab bar, but I am not seeing an actual tab bar:
Update: I realize that I'm not actually launching the xib file in the .m file. Anyone know how I can do this properly?
There are various different solutions for adding a custom set of buttons to a custom tab bar controller subclass. I've done it years ago following this guide: http://idevrecipes.com/2010/12/16/raised-center-tab-bar-button/.
The idea is to add a custom UIView over the tab bar of your UITabBarController subclass. The CustomTabBarController class doesn't have to have a xib. Instead, I have a subclass of UIView that can either be programmatically laid out, or created using a xib for a UIView. Here's the header file for my CustomTabBarView class:
#interface CustomTabBarView : UIView
{
CALayer *opaqueBackground;
UIImageView *tabBG;
IBOutlet UIButton *button0;
IBOutlet UIButton *button1;
IBOutlet UIButton *button2;
NSArray *tabButtons;
int lastTab;
}
#property (nonatomic, weak) id delegate;
-(IBAction)didClickButton:(id)sender;
You'll either connect the desired buttons to button0, button1, button2, etc in the xib file, or do it programmatically on init for the view. Note that this is the UIView subclass.
In CustomTabBarView.m:
-(IBAction)didClickButton:(id)sender {
int pos = ((UIButton *)sender).tag;
// or some other way to figure out which tab button was pressed
[self.delegate setSelectedIndex:pos]; // switch to the correct view
}
Then in your CustomTabBarController class:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
tabView = [[CustomTabBarView alloc] init];
tabView.delegate = self;
tabView.frame = CGRectMake(0, self.view.frame.size.height-60, 320, 60);
[self.view addSubview:tabView];
}
When the buttons are clicked in the CustomTabBarView, it will call its delegate function, in this case the CustomTabBarController. The call is the same function as if you clicked on a tab button in the actual tab bar, so it will jump to the tabs if you have set up the CustomTabBarController correctly like a normal UITabBarController.
Oh, on a slightly separate note, the correct way to add a custom xib as the interface for a subclass of UIView:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([self class]) owner:self options:nil];
UIView *mainView = [subviewArray objectAtIndex:0];
//Just in case the size is different (you may or may not want this)
mainView.frame = self.bounds;
[self addSubview:mainView];
}
return self;
}
In the xib file, make sure the File's Owner has its Custom class set as CustomTabBarView.
I am having a problem with my UITableView in iOS7. Initially, the data loads in just fine, and I get output in my console that proves that the cells are speaking to the data source correctly, but as soon as I tap anywhere on the table, the cells disappear and the table goes blank. The height of the cells in the empty UITableView seem to be honoring the height my custom prototype cell (410px), but all the data in the cells vanish, and the empty table view acts like it only has one cell in it (like its default state before it gets hooked up to the delegate).
I am using Storyboards for this app.
To get a little context, this app is similar to the iphone Instagram app, and I am using this application as way to learn iOS 7 development. I have been banging my head up against a wall trying to solve this issue, and I can't find any online resources that can help me solve this, so I wanted to ask all the smart peeps on Stack Overflow.
I have prepared a graphic that helps you see the problem
higher resolution version here
Here is my TableViewController code:
#interface PA_PhotoTableViewController ()
#property (nonatomic, copy) NSArray *photos;
#end
#implementation PA_PhotoTableViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.photos = [[PA_PhotoStore sharedPhotoStore] allPhotos];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return [[[PA_PhotoStore sharedPhotoStore] allPhotos] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PA_PhotoCell *cell = [tableView dequeueReusableCellWithIdentifier:#"PhotoCell" forIndexPath:indexPath];
PA_Photo *photo = (self.photos)[indexPath.row];
cell.photoTitle.text = photo.title;
cell.photoOwnerName.text = [NSString stringWithFormat:#"%#", photo.owner];
cell.photoLikes.text = #"99";
// Photo Image URL
NSURL *photoImageURL = [NSURL URLWithString:photo.image_full_url];
[cell.photoImage sd_setImageWithURL:photoImageURL placeholderImage:[UIImage imageNamed:#"lightGraySpinningLoader.gif"]];
// Photo Owner Image
[cell.photoOwnerImage sd_setImageWithURL:photoImageURL placeholderImage:[UIImage imageNamed:#"lightGraySpinningLoader.gif"]];
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// This code never gets called when I try to tap on a cell
NSLog(#"A row was selected");
}
- (void)dealloc {
NSLog(#"dealloc called in PA_PhotoTableViewController");
}
and here is the custom cell code PA_PhotoCell (consolidated .h & .m files):
#interface PA_PhotoCell : UITableViewCell
#property (nonatomic, weak) IBOutlet UIImageView *photoImage;
#property (nonatomic, weak) IBOutlet UILabel *photoTitle;
#property (nonatomic, weak) IBOutlet UILabel *photoOwnerName;
#property (nonatomic, weak) IBOutlet UIImageView *photoOwnerImage;
#property (nonatomic, weak) IBOutlet UILabel *photoLikes;
#property (nonatomic, weak) IBOutlet UILabel *photoTimestamp;
#property (nonatomic, weak) IBOutlet UILabel *photoComments;
#end
#implementation PA_PhotoCell
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
[super setSelected:selected animated:animated];
NSLog(#"in set selected");
}
-(void)setHighlighted:(BOOL)highlighted {
NSLog(#"in set highlighted");
}
You can see a few NSLog() calls to help me see if anything is getting called.
Where am I going wrong? The end goal is to click on one of the TableViewCell instances and launch a UINavigationController, I know how to do that, but I can't move on to that step until I figure out why my UITableView won't scroll, and why it disappears when I click on it!
EDIT: After much testing, debugging and experimentation, I have been able to conclude that the problem is actually not with the UITableView at all, and it is, in fact, a problem with how the UITableView is being loaded into its parent view. I still haven't found a solution to my problem, but I am getting closer to finding the cause. Here is what I have discovered:
First, when any of the UIButtons at the bottom of the screen are tapped (see photo reference), it loads the relevant instance of UIViewController into a UIView called placeholderView. When I run my problematic UITableView OUTSIDE of this UIView (where the UITableViewController is acting on its own, not embedded within another UIView) then the table works perfectly, it scrolls, it revives click events, and so on. So as soon as I load the UITableView into the UIView, the UITableView becomes unresponsive (it doesn't scroll or receive tap events) and any attempt to interact with it, the UITableView goes completely blank. My debugging session concludes that the NSArray *photos never gets reset to nil, or manipulated in anyway, the table just goes blank.
So does anyone have any ideas on what would cause a UITableView to do this when being loaded into a generic UIView? All the other views that get loaded into this generic UIView are responsive, and behave as expected. Its just this UITableView that is giving me problems.
If you review the graphic I attached to this post (above), you will see that I am using what appears to be a UITabBarView, but it is, in fact, just a generic view with UIButtons inside. The reason I decided to craft my own "UITabBarView look-alike" instead of using the ready-made UITAbBarView class was because I wanted to give custom functionality to the "menu" button on the bottom left (I want a nice UIView to slide in from the left, and stop about 60 pixels from the right of the screen when the "menu" button is tapped, and I can't figure out how to customize the behavior of the UITabBarView, so I opted for this approach.
Here is the code that is actually loading the UITableViewController into the subview (via a CustomStoryboardSegway):
// PA_HomeViewCustomStoryboardSegue.m
#import "PA_HomeViewCustomStoryboardSegue.h"
#import "PA_HomeViewController.h"
#implementation PA_HomeViewCustomStoryboardSegue
// to create a custom segue, you have to override the perform method
-(void)perform {
// get the source and destination view controllers
PA_HomeViewController *segueSourceController = (PA_HomeViewController *)[self sourceViewController];
UIViewController *destinationController = (UIViewController *)[self destinationViewController];
for (UIView *view in segueSourceController.placeholderView.subviews){
[view removeFromSuperview];
}
segueSourceController.currentViewController = destinationController;
[segueSourceController.placeholderView addSubview:destinationController.view];
}
#end
and here is the header file for my PA_HomeViewController (the view the contains the "placeholderView" which is the target view that loads the various UIViewControllers after the user has tapped the UIButtons at the bottom of the view (similar to a TabBarView) :
#interface PA_HomeViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIView *placeholderView;
#property (weak, nonatomic) UIViewController *currentViewController;
#end
I am hoping that I am just missing something obvious in the way that I am loading the UITableView into the placeholderView, and something in there is causing the UITableView to go completely blank.
When you display the UITableView in a different view, you must always make sure that the view controller which "hosts" the UITableView has a strong reference to its controller. In your case, the data source for the UITableView seems to be deallocated after adding the UITableView as subview.
Changing the currentViewController property from weak to strong should fix your problem.
In swift you need to declare viewcontroller object globally that would result in Strong, in case if you declare locally it results in keep disappearing the cells.
e.g.
var refineViewController : RefineViewController?
then you can access that controller using below code that would not result in disappearing cells.
func showRefineView(isFindHomeTab isFindHomeTab : Bool){
refineViewController = RefineViewController(nibName: String(BaseGroupedTableVC),bundle : nil)
refineViewController!.refineSearchDelegate = self
refineViewController!.view.frame = CGRectMake(0, -490, self.view.frame.size.width, self.view.frame.size.height)
UIView.animateWithDuration(0.3, delay: 0.0, options: .CurveEaseOut, animations:
{
self.refineViewController!.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)
self.refineViewController!.isFindHomeTab = isFindHomeTab
}, completion: nil)
self.view.addSubview(refineViewController!.view)
}
I experienced the exact same problem. The issue was that I was using a custom datasource class (called tableVCDataSource), and was setting the tableView's dataSource incorrectly in the ViewController class. I was doing:
override func viewDidLoad() {
mainTableView.dataSource = TableVCDataSource()
}
when I should have been doing:
fileprivate var tableVCDataSource: TableVCDataSource?
override func viewDidLoad() {
tableVCDataSource = TableVCDataSource()
mainTableView.dataSource = tableVCDataSource
}
This solved my issue.
I've managed to setup a custom UIView class with a nib.
My .h looks like
#interface MyView : UIView <UITextFieldDelegate>
#property (nonatomic, weak) IBOutlet UITextField *textField;
#property (nonatomic, strong) MyView *topView;
And .m
#implementation MyView
NSString *_detail;
-(id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])&&self.subviews.count==0){
MyView *v = [[[NSBundle mainBundle] loadNibNamed:#"MyView" owner:self options:nil] objectAtIndex:0];
self.textField = v.textField;
if (self.topView == nil)self.topView = self;
v.topView = self.topView;
[self addSubview:v];
}
return self;
}
-(NSString *)topDetail{
return _detail;
}
-(NSString *)detail{
return [self.topView topDetail];
}
-(void)setTopDetail:(NSString *)detail{
_detail = detail;
}
-(void)setDetail:(NSString *)detail{
[self.topView setTopDetail:detail];
}
- (BOOL)textFieldShouldReturn{
//here I show an UIAlertView using self.detail for the message
}
Note: The setup I have works exactly how I want it to.
The problem
What I would like to do is remove my manual detail methods and turn NSString *_detail into #property (...)NSString *detail
When I try it with the #property, then within my ViewController if i call
myView.detail = someString, myView will be referring to the top most view. Then if textFieldShouldReturn gets called because of user interaction, then it calls the nested MyViews _detail which has not been set.
What I want:
To not have to write extra code for access to _detail regardless of where I'm accessing it from. I want to merely declare the property and go on with my usual coding.
Your problem is that you're trying to keep the a class reference, topView, with an object property.
In other words every objects' topView is the object itself, which makes no sense.
Your definition should be:
#interface MyView : UIView <UITextFieldDelegate>
// Class "properties"
+ (instancetype)topview;
+ (void)setTopView:(UIView *)topView;
// Object properties
#property (nonatomic, weak) IBOutlet UITextField *textField;
#property (nonatomic, strong) NSString *detail;
Now you can keep track of the topView:
static MyView * _topView;
#implementation MyView
+ (instancetype)topView {return _topView}; // You could also create one here lazily
+ (void)setTopView:(UIView *)topView { _topView = topView };
-(id)initWithCoder:(NSCoder *)aDecoder{
if ((self = [super initWithCoder:aDecoder])&&self.subviews.count==0){
JUITextFieldHint *v = [[[NSBundle mainBundle] loadNibNamed:#"JUITextFieldHint" owner:self options:nil] objectAtIndex:0];
self.textField = v.textField;
if ([MyView topView] == nil)[MyView setTopView:self];
v.topView = self.topView;
[self addSubview:v];
}
return self;
}
No more need for manual setters and getters. Now you can use your detail property, either with anyInstance.detail or [MyView topView].detail, or even MyView.topView.detail if you like dots like me ;)
You're init method still looks weird but should work. Check Apples init template.
Lastly, textField can be weak as long as it has a superview, otherwise make it strong.
My xib contained one UIView (no controller). I had the UIView set to MyView for the class.
I changed the UIView back to just UIView then set File's Owner to MyView. This solved issues of recursion (which is why I had such a weird setup in the first place) and caused my variables and IBOutlets to be linked up properly.
Credit goes to How do I create a custom iOS view class and instantiate multiple copies of it (in IB)? and some of the comments which I missed the first couple times I read through it.
I know this is quite straight forward but after too much hair-pulling I am nowhere near solution.
I have seen tutorials explaining how to create view using XIB and all. But none of them address the situation that I have here.
I have an XIB file, a custom UIView subclass that has few labels and buttons. The UIView subclass is reusable, and that is the reason I can't have outlets inside any single View controller. As a result I store individual controls (subviews) of this view inside my custom UIView itself. This is logical, as no view controller should own the subviews of this custom view which is to be included in every view controller.
The problem is, I don't know how to initialize the entire UI fully.
Here is my code for UIView Subclass:
#interface MCPTGenericView : UIView
+(id)createInstance : (bool) bPortrait;
#property (weak, nonatomic) IBOutlet UIView *topView;
#property (weak, nonatomic) IBOutlet UIView *titleView;
#property (weak, nonatomic) IBOutlet UILabel *titleLabel;
#property (weak, nonatomic) IBOutlet UIButton *logoButton;
#property (weak, nonatomic) IBOutlet UITextField *searchTextField;
#property (weak, nonatomic) IBOutlet UIButton *menuButton;
#end
Later on, I also plan to use this same XIB file for landscape orientation of this UIView too, and I plan to use the same above outlets with landscape oriented controls in same XIB.
And here is the implementation:
#implementation MCPTGenericView
//#synthesize topView, titleLabel, titleView;
+(id)createInstance : (bool) bPortrait
{
UIView * topLevelView = nil;
MCPTGenericView * instance = [MCPTGenericView new];
NSArray * views = [[NSBundle mainBundle] loadNibNamed:#"MoceptGenericView" owner:instance options:nil];
int baseTag = (bPortrait)?PORTRAIT_VIEW_TAG_OFFSET:LANDSCAPE_VIEW_TAG_OFFSET;
// make sure customView is not nil or the wrong class!
for (UIView * view in views)
{
if (view.tag == baseTag)
{
topLevelView = view;
break;
}
}
instance.topView = (MCPTGenericView *)[topLevelView viewWithTag:baseTag + 1];
instance.searchTextField = (UITextField *)[topLevelView viewWithTag:baseTag + 2];
instance.menuButton = (UIButton *)[topLevelView viewWithTag:baseTag + 3];
instance.logoButton = (UIButton *)[topLevelView viewWithTag:baseTag + 4];
instance.titleView = [topLevelView viewWithTag:baseTag + 5];
instance.titleLabel = (UILabel *)[topLevelView viewWithTag:baseTag + 6];
return instance;
}
-(id)initWithCoder:(NSCoder *)aDecoder
{
if ((self = [super initWithCoder:aDecoder]))
{
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:#"MCPTGenericView" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
-(void)awakeFromNib
{
[super awakeFromNib];
[self addSubview: self.titleView];
[self addSubview:self.topView];
}
- (id) init
{
self = [super init];
if (self)
{
[[NSBundle mainBundle] loadNibNamed:#"MCPTGenericView" owner:self options:nil];
[self addSubview:self.topView];
[self addSubview:self.titleView];
}
return self;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
// Initialization code
[[NSBundle mainBundle] loadNibNamed:#"MCPTGenericView" owner:self options:nil];
[self addSubview:self.topView];
[self addSubview:self.titleView];
}
return self;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Something that worked:
I succeeded in calling initWithFrame:frame from my viewcontroller. That way, I could see all controls properly initialized. But then, why should I be supplying a frame if I have already drawn an XIB? Shouldn't loadNibNamed be handling frame setting and layout stuff since that is the intended use of XIBs?
I am also baffled at the way loadNibNamed needs an owner object. Why do we already need an object to get the same object from XIB? That too, a half-baked one?
Please help...
What was baffling me was the way loadnibnamed loses xib layout & outlet information. I finally found a way to achieve it.
Here is a recap of what works:
1) Suppose MyCustomView is your custom view class - you design it and its subviews as part of XIBs. You do this via interface builder, so self-explanatory.
2) Add MyCustomView.h and MyCustomView.m (boilerplate) via Xcode -> File -> New -> Objective C Class.
3) Next, within MyCustomView.xib, set File's Owner = MyCustomView (class name just added). Do not touch top most View's custom class - leave it as UIView. Else it will end up in recursion!!!
4) In MyCustomView.h, create few outlets corresponding to subviews within MyCustomView.xib.
Such as:
#property (weak) IBOutlet UILabel * label1;
#property (weak) IBOutlet UIButton * button1;
5) Go to MyCustomView.xib. Select each subview (label, button), right click, drag from "New Referencing Outlet" and drag it up to File's Owner.
This will popup a list of outlets matching the subview's type from where you have dragged. If you dragged from a label, it will pop up label1, and so on. This shows that all you did up to this step is correct.
If you, on the other hand, screwed up in any step, no popup will appear. Check steps, especially 3 & 4.
If you do not perform this step correctly, Xcode will welcome you will following exception:
setValue:forUndefinedKey: this class is not key value coding-compliant for the key
6) In your MyCustomView.m, paste / overwrite following code:
-(id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self)
{
NSString * nibName = #"MyCustomView";
[[[NSBundle mainBundle] loadNibNamed:nibName owner:self options:nil] firstObject];
[self addSubview:self.labelContinentName];
}
return self;
}
This step is crucial - it sets your outlet values (label1, button1) from nil to tangible subviews, and most importantly, sets their frame according to what you have set within MyCustomView.xib.
7) In your storyboard file, add view of type MyCustomView - just like any other view:
Drag a UIView in your View Controller main view rectangle
Select the newly added view
In Utilities -> Identity Inspector, set custom class value = MyCustomView.
It should be up & running no problem!
loadNibNamed does not handle frame setting, it only loads content and makes the objecet available to your code. initWithFrame: must be called to insert a new object to the view heirarchy of a window.
I am making an app with dynamic tables inside of it. I had managed to let it work with the table inside the MainviewController, but now i am stuck. I tried to put the table code in its own class.
strange thing is that it gives no errors.
This is what i do:
MainviewController.m:
CGRect frame;
frame.origin.x = self.scrollView.frame.size.width * 0;//0 will later be i
frame.origin.y = 0;
frame.size.width = self.scrollView.frame.size.width;
TellerTable *tellerTable = [[TellerTable alloc] initWithFrame:frame style:UITableViewStyleGrouped];
[tellerTable loadWithJsonData:JSONArray];
[tellerTable setDataSource:tellerTable];
[self.view addSubview:tellerTable];
[tellerTable reloadData];
TableTeller.m:
-(void)loadWithJsonData:(NSArray *)JA
{
self.JSONArray = JA;
tableview = self;
self.delegate = self;
self.dataSource = self;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
NSMutableArray *TellerItems = [JSONArray objectAtIndex:section];
return TellerItems.count;
}
(the rest to make the table cellForRowAtIndexPath etc..)
TellerTable.h
#interface TellerTable : UITableView <UITableViewDataSource, UITableViewDelegate>
{
UITableView *tableview;
}
#property (nonatomic, retain) IBOutlet UITableView *tableview;
#property (nonatomic, retain) IBOutlet NSArray *JSONArray;
-(void)loadWithJsonData:(NSArray *)JA;
#end
Strange thing is, it goes to loadwithJSON, then it calls numberOfRowsInSection but after that it stops. It doesn't add the table to the scrollview. What am i doeing wrong/forget?
I think you should change your approach .. you have created a subclass of UITableView and then another tableView property on the same then you are assigning it to self all feels wrong even if it works. I dont see any use of subclassing here you are not customising the table so why are you subclassing it?. Add a tableView(drag and drop) to xib or storyboard then connect outlet to mainviewcontroller. You may be able to make it work somehow using this method but its errorprone,less readable etc etc