Linking UIView subclass and ViewController - iphone

I have a UIView subclass that I want to appear on an xib created from a ViewController class.
My UIView class is called Tag and the other ViewController
// Tag.h
UIView *view;
// Tag.m
if (self) {
[[NSBundle mainBundle] loadNibNamed:#"ViewController" owner:self options:nil];
[self addSubview:self.view];
}
return self;
Am I close? It doesn't seem to be linking

Subclass :MyView
.h
interface MyView : UIView
#end
To add in view controller : MainViewController
#import <UIKit/UIKit.h>
#import "MyView.h"
#interface MainViewController : UIViewController
#property (nonatomic, weak) IBOutlet MyView *viewObj;
#end
in Xib
Connect the outlet.

You can do this from xib file write Tag on Class field

Related

How to add a popup UIView (of a customized class) on a UIViewController

I'm new on this, and I would like to get some advice because I don't know what I'm doing wrong.
I want to make an app in xcode, with a UIView with some items, and when you do something, another UIView (smaller than the first) pops up above the first UIView. The popup UIView would be a customized class.
I have started with the UIViewController template and the initial UIView, and I have linked all the items in the .storyboard, and it works. But when I create my own UIView class (from objective-C class), put the second UIView over the first in the storyboard and link it to my class, something goes wrong.
The UIView appears, but when I try to set it to hidden, it doesn't answer. It's like it's not receiving the messages, so I think I don't link it well programmatically and just appears because of the storyboard.
I don't know if I have to create another UIViewController instead of the UIView, or if this is the correct path.
Can anybody explain me a little, or just write a little code snippet with the instantiation of the second view and adding it?
Lots of thanks!!
(I paste some code, of the declaration in .h and instantiation in .m)
#import <UIKit/UIKit.h>
#import "EditView.h"
#interface ReleaseViewController : UIViewController <UIWebViewDelegate, UISearchBarDelegate> {
IBOutlet UIWebView *web;
IBOutlet UISearchBar *search;
IBOutlet EditView *evHack;
}
#property (nonatomic, retain) IBOutlet UIWebView *web;
#property (nonatomic, retain) IBOutlet UISearchBar *search;
#property (nonatomic, retain) IBOutlet EditView *evHack;
#end
- (void)viewDidLoad
{
[super viewDidLoad];
search.delegate = self;
web.delegate = self;
evHack = [evHack initWithFrame:CGRectMake(0, 44, 320, 377)];
[evHack setHidden:YES];
}
EditView Class (I still have nothing):
#import <UIKit/UIKit.h>
#interface EditView : UIView
#end
#import "EditView.h"
#implementation EditView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
NSLog(#"View created");
}
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
initWithFrame only works when you alloc/init an app. If its already initialized, in this case by the storyboard, just set its frame:
evHack.frame = CGRectMake(0,44, 320, 377);
I don't know what it looks like in IB, But setting its frame in code may be redundant if you set it in IB too. To check whether evHack is hooked up right, NSLog evHack in viewDidLoad. If you get nil back, it's not hooked up right.

Loading nib view into another nib view

I have two UIViews, let me called them as A and B. A is the container of B and each one is created using Interface Builder.
I need to add my B view into A using interface builder, like this image:
The problem is that the view is loaded but it's empty, when I debugged it I saw that every component is created.
The B UIView class definition is:
B.h (ISMSliderCustomizable.h)
#interface ISMSliderCustomizable : UIView {
IBOutlet UIView *view;
IBOutlet UISlider *slider;
IBOutlet UILabel *minLabel;
IBOutlet UILabel *maxLabel;
IBOutlet UIImageView *tooltip;
IBOutlet UILabel *sliderValue;
}
#property (nonatomic, retain) IBOutlet UIView *view;
#property (nonatomic) float min;
#property (nonatomic) float max;
#property (nonatomic) float initialValue;
#end
B.m (ISMSliderCustomizable.m)
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self)
{
[[NSBundle mainBundle] loadNibNamed:#"ISMSliderCustomizable" owner:self options:nil];
[self addSubview:self.view];
}
return self;
}
- (void)awakeFromNib {
[super awakeFromNib];
[[NSBundle mainBundle] loadNibNamed:#"ISMSliderCustomizable" owner:self options:nil];
[self addSubview:self.view];
}
I need to add a UIView into another UIView which every UIView is created with IB.
I think the problem is UIView in b.xib is 320x480
but View B in a.xib is not
so it's not empty,it just out of range

UITextView in tableHeaderView can't find delegate

I have a UITableView tableHeaderView defined in a nib file. The tableHeaderView contains a UITextView. The UITextView's delegate is set to the UITableViewController and the UITableViewController supports the UITextViewDelegate protocol. The UITableViewController is setting various properties in the tableHeaderView including the UITextView's text and that all works fine. But textFieldShouldReturn:(UITextField *)textField in the UITableViewController never gets called and I can't get the keyboard to close after I've tapped the UITextView.
I've tried every suggestion I can find in the docs and various posts, but no luck. None of the examples I've found have this specific case of a UITextView inside a tableHeaderView.
Is there something special I need to do. Has anyone done this successfully?
I've tried setting the UITextView delegate in code, but no luck.
#class Decision;
#interface DecisionDetailViewController : UITableViewController <UINavigationControllerDelegate, UITextViewDelegate> {
Decision *decision;
UIView *tableHeaderView;
UITextView *nameTextField;
}
#property (nonatomic, retain) Decision *decision;
#property (nonatomic, retain) IBOutlet UIView *tableHeaderView;
#property (nonatomic, retain) IBOutlet UITextView *nameTextField;
#end
#implementation DecisionDetailViewController
#synthesize decision;
#synthesize tableHeaderView;
#synthesize nameTextField;
- (void)viewDidLoad {
self.navigationItem.rightBarButtonItem = self.editButtonItem;
if (tableHeaderView == nil) {
[[NSBundle mainBundle] loadNibNamed:#"DecisionDetailHeader" owner:self options:nil];
self.tableView.tableHeaderView = tableHeaderView;
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[nameTextField resignFirstResponder];
return YES;
}
textFieldShouldReturn is a method of UITextFieldDelegate and not UITextViewDelegate. Thats probably your problem.

How can I update my view controller's UILabel through another class?

I'm my class i've added a an instance of my view controller, created a property and then synthesized it in my implementation file. I am trying to update the UIlabel in the view controller like this,
NSString *currentChar = [[NSString alloc] initWithFormat:#"%c", ch];
viewController.outputLabel.text = currentChar;
[currentChar release];
My problem is that everything builds without any errors or warnings but the label just doesn't get updated, what am I doing wrong. I'd really appreciate some help on this one.
Are you sure you're referencing the existing viewController and you didn't instantiate a new one? Your property is not declared as copy, correct?
textProcessor.h / .m
#interface textProcessor : NSObject {
MainViewController *mainView;
}
#property (retain) MainViewController *mainView;
#end
#implementation textProcessor;
#synthesize mainView;
MainViewController.h / .m
#interface MainViewController : UIViewController {
UILabel *myLabel;
}
#property (retain) UILabel myLabel;
#end
#implementation MainViewController
#synthesize myLabel;
When you are initializing your textProcessor class, and you set the value for "mainView" like
-(void)viewDidLoad {
[super viewDidLoad];
textProcessor *proc = [[textProcessor alloc] init];
proc.mainView = self;
//note that you are not doing this:
//MainViewController *mainView = [[MainViewController alloc] init];
//proc.mainView = mainView;
//that was creating a new instance variable instead of using self, the existing one
[textProcessor release];
}
Have you created your label in IB? If you are using IB you have to create an IBOutlet for your UILabel. You then make a connection between the UILabel in IB to your IBOutlet in your class.
Have you tried calling the setNeedsDisplay method on the view? Also you may want to try using the setText method instead of assigning directly to the property.

Do I need to create variables and link IBOutlet for every UIVIew?

I have a View Controller that is swapping UIView objects in and out. There is the potential to have hundreds of different views, each with their own behaviors.
Within my current MainWindow.xib file I currently have:
File's Owner UIApplication
First Responder UIResponder
AppDelegate AppDelegate
-Cover Cover
Window UIWindow
Table of Contents TableOfContents
page1 Page1
page2 Page2
page...n Page...n
The AppDelegate declares the window and the viewController. It's pretty basic.
MainViewController.h
#import <UIKit/UIKit.h>
#class TableOfContents, Page1;
#interface MainViewController : UIViewController {
TableOfContents *tableOfContents;
Page1 *page1;
Page2 *page2;
Page...n *page...n;
}
#property (nonatomic, retain) IBOutlet TableOfContents *tableOfContents;
#property (nonatomic, retain) IBOutlet Page1 *page1;
#property (nonatomic, retain) IBOutlet Page2 *page2;
#property (nonatomic, retain) IBOutlet Page...n *page...n;
-(IBAction)funcGoToPage:(id)sender;
#end
MainViewController.m
#import "MainViewController.h"
#import "TableOfContents.h"
#import "Cover.h"
#import "Page1.h"
#import "Page2.h"
#import "Page...n.h"
#implementation MainViewController
#synthesize page1, page2, page...n tableOfContents;
#synthesize pageID, pagesPathFile, pagesPath;
-(IBAction)funcGoToPage:(id)sender{
//[[self view] removeFromSuperview];
[self.view addSubview:self.tableOfContents];
}
The corresponding UIView classes are pretty bare at the moment so I'll refrain from posting them.
Right now funcGoToPage is just bringing up tableOfContents. Eventually I'll have it go different places depending on what was clicked.
Currently each page is set up as an IBOutlet and linked from the MainViewController to the appropriate UIView in Interface Builder. Done this way each page will have to be set up as a variable and linked to in IB creating a hubub of variables, outlets and connections.
My question is: Is there a way to create these connections on the fly so that I can swap them in using my funcGoToPage function without setting them up as an IBOutlet?
When a nib is loaded all of its content is loaded. If you have lots of views in one nib you'll quickly run out of memory.
I would put each page in a seperate nib and then load the nib when required:
[[NSBundle mainBundle] loadNibNamed:#"nibNameWithoutExtension" owner:self options:nil];
For this to work:
add an IBOutlet, eg newPage, to whatever self refers to
set the File Owner in nibNameWithoutExtension to whatever self refers to
join the view in nibNameWithoutExtension to the newPage outlet of File Owner