Loading nib view into another nib view - iphone

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

Related

Linking UIView subclass and ViewController

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

Custom UINavigationController with button

I would like the NavigationBar to behave the same but would like to change the appearance of it. I've found so many ways of doing this online but I'm not sure which one is the best result for iOS 5.0. The navigation bar will look like this:
Since you are targeting iOS 5 i would definitely go for customizing UINavigationBar using the Appearance proxy. Then you can easily set your own images and they will apply to all navigation bars in your application without subclassing.
You can also customize the buttons in the navigation bar by customizing UIBarButtonItem. There are method like backButtonBackgroundImageForState:barMetrics: for the back button and backgroundImageForState:barMetrics: for the other buttons.
I had been looking for this thing for ages, too, without finding a straightforward solution! Thanks to an friend of mine, and sample codes, we made it with a custom navigation bar class that can be imported into any view controller class.
The .h file:
#import <UIKit/UIKit.h>
#interface NATitleBar : UIView {
NSInteger tag;
}
#property ( nonatomic) IBOutlet UIImageView *imageView;
#property ( nonatomic) IBOutlet UILabel *label;
#property ( nonatomic) IBOutlet UIButton *back;
#property ( nonatomic) IBOutlet UIButton *home;
/**
* Supports UIButton-style adding targets
*/
#end
The .m file:
#import "NATitleBar.h"
#implementation NATitleBar
#synthesize imageView;
#synthesize label;
#synthesize back;
#synthesize home;
- (id)initWithFrame:(CGRect)frame {
self = [super initWithFrame:frame];
if (self) {
NSArray *views = [[NSBundle mainBundle] loadNibNamed:#"NATitleBar" owner:self options:nil];
[self addSubview:[views objectAtIndex:0]];
// customize the view a bit
//self.imageView.layer.borderWidth = 1.0;
//self.imageView.layer.borderColor = [UIColor colorWithWhite:0.4 alpha:0.4].CGColor;
//self.imageView.clipsToBounds = YES;
//self.imageView.layer.cornerRadius = 5.0;
}
return self;
}
#pragma mark - Overriden Setters / Getters
- (void)setTag:(NSInteger)aTag {
self.back.tag = aTag;
}
- (NSInteger)tag {
return self.back.tag;
}
#end
and then for the Nib file we have the following:
You can add or delete images in the Nib file to make the GUI as you wish.
Now you must import the class into any view controller you wish to have with custom navigation controller, and also define two methods (or one, if you don't want the 'home' button. in .h :
- (void) back;
in .m:
- (void)back {
[self.navigationController popViewControllerAnimated:YES];
}

Issue Subclassing UIView

I've created my own custom view, with its own header and main file and corresponding nib (.xib):
The header file
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#interface PointsBarView : UIView
{
IBOutlet UIView *pointsBarView;
IBOutlet UIView *pointsCounterView;
IBOutlet UILabel *pointsTotalLabel;
UIImageView *handImageView;
UIImageView *powerBarOutlineImageView;
}
#property (nonatomic, retain) IBOutlet UIView *pointsCounterView;
#property (nonatomic, retain) IBOutlet UIView *pointsBarView;
#property (nonatomic, retain) IBOutlet UILabel *pointsTotalLabel;
#property (nonatomic, retain) IBOutlet UIImageView *handImageView;
#property (nonatomic, retain) IBOutlet UIImageView *powerBarOutlineImageView;
#end
I'm synthesizing everything in the main, and then in another UIViewController class I'm trying to load this view. I set up its property:
#property (nonatomic, assign) IBOutlet PointsBarView *pointsBarView;
And am adding as so:
NSArray* nibViews = [[NSBundle mainBundle] loadNibNamed:#"PointsBarView" owner:self options:nil];
pointsBarView = [nibViews objectAtIndex: 0];
[[self view] addSubview:pointsBarView];
How do I access those subviews within my NIB? Do they need to be embedded within the pointsBarView? (pointsBarView references the main view of the NIB, and all the other views are within the pointsBarView). Should they each be a separate piece of the NIB and I need to call addSubview for each one to display?
I should note that if I do NOT connect any of the properties in PointsBarView, the view displays just fine with the code in my UIViewController class. But I want to be able to interact with each view and change properties accordingly. Any help would be great!
The general rule of thumb is: if you load it from code, you connect it up in code.
conversely:
If you instantiate in IB, you can connect outlets and actions in IB.
Here you are loading the view in code, so you have to manually connect them.
If you want to be able to connect stuff in IB add a UIVIew in IB and change the subclass to PointsBarView. IB will magically read the PointsBarView.h file and you should be able to connect outlets, targets and actions.

How do I add a custom view to iPhone app's UI?

I'm diving into iPad development and I'm still learning how everything works together. I understand how to add standard view (i.e. buttons, tableviews, datepicker, etc.) to my UI using both Xcode and Interface Builder, but now I'm trying to add a custom calendar control (TapkuLibrary) to the left window in my UISplitView application which doesn't involve Interface Builder, right? So if I have a custom view (in this case, the TKCalendarMonthView), how do I programmatically add it to one of the views in my UI (in this case, the RootViewController)? Below are some relevant code snippets from my project...
RootViewController interface
#interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate> {
DetailViewController *detailViewController;
NSFetchedResultsController *fetchedResultsController;
NSManagedObjectContext *managedObjectContext;
}
#property (nonatomic, retain) IBOutlet DetailViewController *detailViewController;
#property (nonatomic, retain) NSFetchedResultsController *fetchedResultsController;
#property (nonatomic, retain) NSManagedObjectContext *managedObjectContext;
- (void)insertNewObject:(id)sender;
TKCalendarMonthView interface
#class TKMonthGridView,TKCalendarDayView;
#protocol TKCalendarMonthViewDelegate, TKCalendarMonthViewDataSource;
#interface TKCalendarMonthView : UIView {
id <TKCalendarMonthViewDelegate> delegate;
id <TKCalendarMonthViewDataSource> dataSource;
NSDate *currentMonth;
NSDate *selectedMonth;
NSMutableArray *deck;
UIButton *left;
NSString *monthYear;
UIButton *right;
UIImageView *shadow;
UIScrollView *scrollView;
}
#property (readonly,nonatomic) NSString *monthYear;
#property (readonly,nonatomic) NSDate *monthDate;
#property (assign,nonatomic) id <TKCalendarMonthViewDataSource> dataSource;
#property (assign,nonatomic) id <TKCalendarMonthViewDelegate> delegate;
- (id) init;
- (void) reload;
- (void) selectDate:(NSDate *)date;
Thanks in advance for all your help! I still have a ton to learn, so I apologize if the question is absurd in any way. I'm going to continue researching this question right now!
Assuming you have initialized the custom UIView, you need to add it as a subview of the viewController's view.
- (void)addSubview:(UIView *)view
So an example would be if you have a plain viewController called myVC, which has simply a blank white UIView as its view, you would say this:
CGRect customViewsFrame = CGRectMake(10, 30, 5, 2);
MyCustomView *myView = [[MyCustomView alloc] initWithFrame:customViewsFrame];
[[myVC view] addSubview:myView];
[myView release]; //Since you called "alloc" on this, you have to release it too
Then it will show up in the viewController's view, taking up the space indicated by the CGRect.
The CGRect's coordinates specify a location in the local coordinate system of the superview you are adding to, if I'm not mistaken.
CGRect CGRectMake (CGFloat x, CGFloat y, CGFloat width, CGFloat height);
I'm not booted into Mac OS X so I can't verify this completely, but this is your general pattern:
RootViewController.h:
...
#interface RootViewController : UITableViewController <NSFetchedResultsControllerDelegate>
{
...
TKCalendarMonthView* calendarView;
...
}
...
#property (nonatomic, retain) TKCalendarMonthView* calendarView;
...
RootViewController.m:
...
#synthesize calendarView;
...
- (void)dealloc
{
...
[calendarView release];
...
}
...
- (void)viewDidLoad
{
...
TKCalendarMonthView* aCalendarView = [[TKCalendarMonthView alloc] init]; // <-- possibly initWithFrame here
self.calendarView = aCalendarView;
[aCalendarView release];
[self addSubview:self.calendarView];
...
}

How can I store UIButtons in an array?

I have a method tied to four buttons. I want to create an array containing each button, and later retrieve and interact w/ a button from the array. The code I was tinkering with below. When I try to get a button from the array and send it a message, it goes kablooie.
Any thoughts on what I'm doing wrong?
Hack_DumpViewController.h
#import <UIKit/UIKit.h>
#interface Hack_DumpViewController : UIViewController {
IBOutlet UIButton *redButton;
IBOutlet UIButton *greenButton;
IBOutlet UIButton *blueButton;
IBOutlet UIButton *yellowButton;
NSArray *buttonMapping;
}
- (IBAction) changeToYo:(id)sender;
#property (nonatomic, retain) UIButton *redButton;
#property (nonatomic, retain) UIButton *greenButton;
#property (nonatomic, retain) UIButton *blueButton;
#property (nonatomic, retain) UIButton *yellowButton;
#property (nonatomic, retain) NSArray *buttonMapping;
#end
Hack_DumpViewController.m
#import "Hack_DumpViewController.h"
#implementation Hack_DumpViewController
#synthesize redButton;
#synthesize greenButton;
#synthesize yellowButton;
#synthesize blueButton;
#synthesize buttonMapping;
- (IBAction) changeToYo:(id)sender {
NSLog(#"changing numbers!");
for (UIButton *b in buttonMapping) {
[b setTitle:#"yo!"];
}
NSLog(#"changed to numbers!");
}
- (void)viewDidLoad {
buttonMapping = [[NSArray alloc] initWithObjects:greenButton, redButton, yellowButton, blueButton, nil];
}
[NSArray arrayWithObjects:...] returns an autoreleased array, so by the time you use it, it no longer exists and you end up messaging an invalid pointer. What you want is [[NSArray alloc] initWithObjects:...] (remembering to release it in your dealloc).
Why not tag the views in interface builder and then treat them like an array, much easier