I am trying to find a way to initialise a view controller which has IBOutlets to a custom view that I have created in a XIB.
Here is the code where I initialise my class:
MyContentViewController *pageContentViewController = [[MyContentViewController alloc] init];
self.pageContent = pageContentViewController;
[pageContentViewController view];
MyContentViewController has the following properties:
#interface MyContentViewController : UIViewController
#property (nonatomic, weak) IBOutlet MyContentView *topLeftView;
#property (nonatomic, weak) IBOutlet MyContentView *topRightView;
#property (nonatomic, weak) IBOutlet MyContentView *bottomLeftView;
#property (nonatomic, weak) IBOutlet MyContentView *bottomRightView;
In MyContentViewController xib I have 4 views which I have set as MyContentView and have hooked the IBOutlet's above to the views in the storyboard.
The custom view is defined as following:
#import <UIKit/UIKit.h>
#interface MyContentView : UIView
#property (nonatomic, strong) IBOutlet UILabel *labelView;
#end
However when I run the application, I get the 4 views I have added in the MyContentViewController xib. However none of them display the UILabel from the custom UIView.
I have added the following in MyContentView:
- (void)awakeFromNib {
NSLog(#"VIEW AWAKE");
}
This get printed out so the views are being loaded. However how can I get them to display using the 4 IBOutlets.
Here is a picture of the app running with the view. There is no labels on the 4 views:
I see that your "MyContentView" has a nib of it's own. Loading a custom UIView class which has a nib layout from another nib is a bit more complex.
I think this blog post contains the answer
Related
I have a UIView with two UILabel that I want to reuse in more than one UIViewController. I use storyboard and I assign a custom class alertView:UIView that I declared.
file AlertRemote.h
#interface AlertRemote: UIView
#property (weak, nonatomic) IBOutlet UILabel *label1;
#property (weak, nonatomic) IBOutlet UILabel *label2;
-(void) setTextLabel;
file AlertRemote.m
-(void) setTextLabel{
[ _label1 setText:#"attention..."];
[_label2 setText:#"the program..."];
}
file Controllo.m
//the view present in the storyboard alertView linked to the uiview
#property (strong, nonatomic)IBOutlet AlertRemote *alertView;
#property AlertRemote *alRemView;
[super viewDidLoad];
_alertView=_alRemView; [_alertView setTextLabel];
[_alertView setTextLabel];
if I put some breakpoints inside setTextLabel the code don't works
thanks!!
In order for the custom alert to work, you need to initialise it.
AlertRemote *alRemView;
alRemView = [[AlertRemote alloc]init];
[alRemView setTextLabel];
I think you are not initializing the property alertview. If you are initializing the property alertview then try to set directly with out using alremView.
I get the famous loaded the "MyController" nib but the view outlet was not set error. However I made sure, that the IBOutlet view is set.
Once the exception is thrown I hit a breakpoint. Below you can see that
All IBOutlets are connected
All IBOutlets are set
When unfolding UIViewController super-class, I can see that _view is 0x00000000 and obviously causes this exception.
Code (header)
#interface InfoDialogViewController : UIViewController
#property (strong, nonatomic) id episode;
#property (strong, nonatomic) NSString *identifier;
#property (strong) IBOutlet UIView *regularSide;
#property (strong) IBOutlet UIView *flippedSide;
#property (weak) IBOutlet UIImageView *episodeCover;
#property (weak) IBOutlet UITextView *episodeTitle;
#property (weak) IBOutlet UITextView *episodeSummary;
- (IBAction)flip:(id)sender;
#end
Some notes
The xib file contains three UIViews on its root level (Flipped, Regular, View)
InfoDialogViewController.m file doesn't contain any methods (I don't do any funky by overriding)
I am using this Controller in combination with addChildViewController.
Anybody has an idea what happens here and how I can fix it? Does ARC play some tricks on me?
Please check:
1. The Class for your View Controller's View should be UIView
2. File owner should be your View Controller
3. Right CLick on File Owner, your view Outlet should be set.
If it is already solved, can u mention what solved your issue?
Every UIViewController of my app is using the UITextField delegates, like:
- (void)textFieldDidBeginEditing:(UITextField*)textField {
self.responder = textField;
}
- (BOOL)textFieldShouldReturn:(UITextField*)textField {
double elementYPosition = self.responder.frame.origin.y;
double elementHeight = self.responder.frame.size.height;
double scrollYPosition = self.scrollView.contentOffset.y;
/* some logic */
}
Now I'm trying to create a base view controller, so I can inherit use it all around, reusing the methods. The responder property in the base view controller works just fine, because the UITextField delegate set its value, but the scrollview is a IBOutlet, and I'm not really sure how to design this base class:
#import "ViewControllerBase.h"
#interface ViewControllerBase ()
#property (weak, nonatomic) UIView* responder;
#property (weak, nonatomic) IBOutlet UIScrollView *scrollView; /* ??? */
#end
#implementation ViewControllerBase
-- methods
#end
In the subclass header:
#property (weak, nonatomic) UIScrollView *scrollView;
In the subclass load method:
self.scrollView = (UIScrollView *)self.view.subviews[0];
But it only works because my view controllers are always in the following format:
UIViewController (self)
UIView (self.view)
UIScrollView (self.view.subviews[0])
UILabel
UIElement
UIElement
UIElement
I have a view controller alertForNeedsClassification as a property in another class, as such:
#interface SCAAppDelegate()
{
HomeScreenViewController * _homeScreenViewController;
NSInteger SCAStatus;
}
#property (strong, nonatomic) PromptClassifyViewController * alertForNeedsClassification;
#end
#implementation SCAAppDelegate
#synthesize alertForNeedsClassification;
#synthesize window = _window;
PromptClassifyViewController's interface looks like this:
#interface PromptClassifyViewController : UIViewController
#property (weak, nonatomic) IBOutlet UILabel *headerTitle;
#property (weak, nonatomic) IBOutlet UITextView *message;
#property (weak, nonatomic) IBOutlet UIButton *notNowButton;
#property (weak, nonatomic) IBOutlet UIButton *classifyButton;
#property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;
#property (weak, nonatomic) IBOutlet UIView *alertView;
#property NSUInteger tag;
#property (weak, nonatomic) IBOutlet id<PromptClassifyViewControllerDelegate> delegate;
- (void)show;
- (void)showFromView:(UIView *)view;
- (IBAction)show:(id)sender;
- (IBAction)dismiss:(id)sender;
- (IBAction)buttonWasPressed:(id)sender;
- (void)setHeaderTitleWithText:(NSString *)text;
#end
I am trying to change the values of IBOutlets message and headerTitle text, like this:
alertForNeedsClassification = [[PromptClassifyViewController alloc] initWithNibName:#"PromptClassifyViewController" bundle:nil];
//[alertForNeedsClassification setDelegate:self];
self.alertForNeedsClassification.headerTitle.text = #"A title";
alertForNeedsClassification.message.text = #"A message";
Then I show alertForNeedsClassification calling a show method (it's like a custom uialertview, but it doesn't subclass from uialertview).
Thing is, no matter how I change it, the text on alertForNeedsClassification.view is always that which is defined in the nib, ie. I can't change it programmatically.
My custom alert view is based on Jeff LaMarche's design: http://iphonedevelopment.blogspot.com/2010/05/custom-alert-views.html
Any ideas what might be going on?
Please be careful when you allocate and initialize the UIView object, especially if you trying to mix using Nib and dynamically generating objects. The best place is within -(void)awakeFromNib or -(void)viewDidLoad
Also, make sure these methods are called. By using -(id)initWithNibName:bundle: only cannot make sure your view to be loaded. Try -(void)addChildViewController and -(void)addSubview: on parentViewController's view to make sure view is loaded after being initialized.
If the text had to be prepared before being loaded, assign it to separate NSString property within PromptClassifyViewController class. Since this property is independent from view being loaded, you can change it's value BEFORE view is appeared. Make sure this text is used and applied to the headerTitle within -(void)show method.
Since you allocate PromptClassifyViewController and access weak referenced headerTitle from self. alertForNeedsClassification, make sure it's not deallocated right afterward.
Usually, weak option is not used for IBOutlet properties. Though it is used when generating outlet connection code by dragging objects from Interface Builder. Try testing your code using strong.
I was assigning values to the IBOutlets before they were alloc'd/initialized. The solution I implemented was to set the values I needed to non-IBOutlet properties (NSStrings in this case) and assign those where needed, in Prompt...Controller's viewDidLoad;
I'm newbie in the iOS development and I'm working in a projecte that it uses iOS5 and Storyboarding. I would like to create a reusable component, like a button, with a custom view (inside the button there will be some images and labels, it will be a big button), to create some of them in the same view of a Storyboard.
So I've created a XIB file (ItemClothes.xib) with a UIButton and inside of it some UIImagesViews and a UILabel. Then I've created a subclass of UIButton (UIItemClothes.h and UIItemClothes.m), with properties for UIImageViews and UILabel components.
UIItemClothes.h
#import <UIKit/UIKit.h>
#import "SCClothes.h"
#interface UIItemClothes : UIButton
#property (nonatomic, strong) IBOutlet UIImageView *imageClothes;
#property (nonatomic, strong) IBOutlet UIActivityIndicatorView *loadingImage;
#property (nonatomic, strong) IBOutlet UIImageView *seasonIconClothes;
#property (nonatomic, strong) IBOutlet UIImageView *categoryIconClothes;
#property (nonatomic, strong) NSString *idClothes;
#property (strong, nonatomic) IBOutlet UILabel *lblProva;
#end
UIItemClothes.m
#import "UIItemClothes.h"
#implementation UIItemClothes
#synthesize imageClothes = _imageClothes;
#synthesize loadingImage = _loadingImage;
#synthesize seasonIconClothes = _seasonIconClothes;
#synthesize categoryIconClothes = _categoryIconClothes;
#synthesize idClothes = _idClothes;
#synthesize lblProva = _lblProva;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self addSubview:[[[NSBundle mainBundle] loadNibNamed:#"ItemClothes" owner:self options:nil] objectAtIndex:0]];
}
return self;
}
#end
Then in the .XIB file I've set the class of UIButton as UIItemClothes, and make the relationships between XIB's UI components and my class properties.
So, after that, in the Storyboard, in the ViewController of one view I've written this code:
UIItemClothes *item = [[UIItemClothes alloc] initWithFrame:CGRectMake(0.0, 0.0, 200.0, 200.0)];
item.lblProva.text = #"test!";
[item.categoryIconClothes setImage:iconCategory];
item.seasonIconClothes.image = iconSeason;
[cell addSubview:item];
As you see, this component will be inside a TableViewCell, and the idea is to put more components (UIItemClothes) inside of it.
The problem is that the component is drawed but any outlet is set as I do in the code above.
Can anyone help me?
Thanks!
well, the problem has been resolved... Instead of having a custom subclass of UIButton, there will be needed a ViewController with all Outlets. Then in the other ViewController (StoryBoard) is initialized this new ViewController