How To Combine 2 UIView into 1 View Controller? - iphone

I have a ViewController with 3 views: Rootview that shows toolbar with UISegmentedControl, tableView, and calendarView.
I have XIB for the rootView, and tableView, but the calendarView doesn't have a XIB.
I need to somehow combine the code to load the calendar view to fit with with this ViewController. Before, I was using the calendarView as its own viewController.
The code for calendarView:
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
calendar = [[TKCalendarMonthView alloc] init];
calendar.delegate = self;
calendar.dataSource = self;
}
return self;
}
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
UIBarButtonItem *actionButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStylePlain target:self action:#selector(dismissCalendarView)];
self.navigationItem.leftBarButtonItem = actionButton;
[actionButton release];
int statusBarHeight = 0;
CGRect applicationFrame = (CGRect)[[UIScreen mainScreen] applicationFrame];
self.view = [[[UIView alloc] initWithFrame:CGRectMake(0, statusBarHeight, applicationFrame.size.width, 300.0)] autorelease];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view.backgroundColor = [UIColor clearColor];
self.title = #"Select Workout";
calendar.frame = CGRectMake(0, 0, calendar.frame.size.width, calendar.frame.size.height);
NSLog(#"%f height", applicationFrame.size.height);
[self.view addSubview:calendar];
[calendar reload];
}
If I put that code directly into this new viewController, it does not respect the UISegmentedControl and just shows up at launch.
Here is the code for the UISegmentedConrol:
- (void)segmentedControl:(SVSegmentedControl*)segmentedControl didSelectIndex:(NSUInteger)index
{
switch (index)
{
case 0:
{
[self.view addSubview: tableView1];
tableView1.hidden = NO;
calendar.hidden = YES;
[calendar removeFromSuperview];
break;
}
case 1:
{
[self.view addSubview: calendar];
tableView1.hidden = YES;
calendar.hidden = NO;
[tableView1 removeFromSuperview];
break;
}
}
}

Does using two different init-Methods fit your needs? initWithNibName:#"nib1" or likewise initWithNibName:#"nib2"??
Otherwise you will need to specify what you want to achieve a little more

Related

Changing the color of a UINavigationBar

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]))
{
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancel)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Send to Twitter"
style:UIBarButtonItemStyleDone
target:self
action:#selector(save)];
}
return self;
}
Another piece of code
- (void)loadView
{
[super loadView];
self.navigationController.navigationBar.tintColor = [UIColor blackColor];
self.view.backgroundColor = [UIColor whiteColor];
self.textView = [[UITextView alloc] initWithFrame:self.view.bounds];
textView.delegate = self;
textView.font = [UIFont systemFontOfSize:15];
textView.contentInset = UIEdgeInsetsMake(5,5,0,0);
textView.backgroundColor = [UIColor whiteColor];
textView.autoresizesSubviews = YES;
textView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[self.view addSubview:textView];
}
I'm trying to change the navigation bar color to black, however no matter what i do here, the color still stays default (Blue). How do you change the color of the bar on top of the view??
Try something like:
self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
This should be done on viewWillAppear: as during init method self.navigationController will be obtained as nil and hence will not produce any effect.
Hope this helps.
If you use a programatic approach (seems like you don't) you could write this code
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
// window initialization
window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
YourViewController *viewController = [[YourViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] \
initWithRootViewController:viewController];
[viewController release];
[[navigationController navigationBar] setTintColor:[UIColor blackColor]];
[window addSubview:navigationController.view];
[self.window makeKeyAndVisible];
return YES;
}
It works for me. Hope it helps! ;)

how we create textField in clicking UIBarButton "add"

- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add:)] autorelease];
}
-(IBAction) add :(id)sender {
}
in view textfield not find the trick is when I click the add textfiled appears in view
You could create the textfield ahead of time add it to your view and set the hidden property to YES and in add you just make it visible by setting hidden to NO.
- (void)loadView
{
UIView * newView = [[UIView alloc] init];
// retaining property
self.myTextField = [[[UITextField alloc] init] autorelease];
myTextField.hidden = YES;
[newView addSubview:myTextField];
self.view = newView;
[newView release];
}
- (IBAction)add:(id)sender
{
myTextField.hidden = NO;
}
Adding a new UITextField every time add is called
- (IBAction)add:(id)sender
{
UITextField * textfieldToAdd = [[[UITextField alloc] init] autorelease];
// ... configuration code for textfield ...
[self.view addSubview:textfieldToAdd];
}

How to programmatically create UIView -> UIViewController -> UITableView

I've been struggling to add a UIView above my UITableViewController. Through searches, reading and experimenting I've determined that instead of a UITableViewController I should just be using a UIViewController. I'm having a hard time making this work for a variety of reasons and I'd like to just start fresh with a better architecture.
Basically I'm looking for sample code / tutorials that could help me create the following completely programmatically (no NIBS):
- Navigation-based Layout
- UIViewController
-- UIView
--- UITableView
--- Etc.
The reason why I want a UIView above my UITableView is I want to be able to add UIViews above my table.
-UPDATE-
Adding code to make this more clear:
JMoviesListViewController.m - UITableViewController subclass
- (void)loadView
{
NSLog(#"loadView called");
UIView *baseView = [[[UIView alloc] init] autorelease];
TISwipeableTableView * aTableView = [[[TISwipeableTableView alloc] init] autorelease];
[aTableView setDelegate:self];
[aTableView setDataSource:self];
[aTableView setSwipeDelegate:self];
[aTableView setRowHeight:54];
[baseView addSubview:aTableView];
self.view = baseView;
[super loadView];
}
- (void)viewDidLoad {
listOfMovies = [[NSMutableArray alloc] init];
UIView *myProgView = (UIView *)self.progView; // progView is a method that returns a UIView
[self.view insertSubview:myProgView aboveSubview:self.tableView];
[self.navigationItem setTitle:#"Now Playing"];
movies = [[Movies alloc] init];
movies.delegate = self;
[movies getMovies:[NSURL URLWithString:apiQuery]];
[super viewDidLoad];
}
- (UIView *)progView {
if (progView == nil)
{
// create a progress view
//x,y,w,h
progView = [[UIView alloc] initWithFrame:CGRectMake(110, 110, 95, 30)];
progView.backgroundColor = [UIColor grayColor];
progView.tag = 1; // tag this view for later so we can remove it from recycled table cells
progView.autoresizingMask = (UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin);
progView.layer.cornerRadius = 5;
UILabel *activityLabel = [[UILabel alloc] init];
activityLabel.text = NSLocalizedString(#"Loading...", #"string1");
activityLabel.backgroundColor = [UIColor grayColor];
activityLabel.textColor = [UIColor whiteColor];
activityLabel.font = [UIFont systemFontOfSize:14];
[progView addSubview:activityLabel];
activityLabel.frame = CGRectMake(5, 2, 70, 25);
UIActivityIndicatorView *activityIndicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite];
[activityIndicator startAnimating];
[progView addSubview:activityIndicator];
activityIndicator.frame = CGRectMake(70, 5, 20, 20);
}
return progView;
}
To be clear, the code works fine, the problem is that the cell lines of the table are "bleeding through" the UIView spinner that is inserted with this line:
[self.view insertSubview:myProgView aboveSubview:self.tableView];
leading me to believe that myProgView is not aboveSubview:self.tableView.
Views and controllers are separate things. You can have a hierarchy of view controllers and a hierarchy of views. But they're not interleaved, as the title of posts suggests (I know, Interface Builder displays them as a single hierarchy, but views and controllers are more like two parallel hierarchies).
Anyway, you can easily have a view controller set up whatever views you want in code. Override the loadView method, create a view that you assign to self.view, then add subviews to that view.
For example:
- (void)loadView
{
UIView *view = [[[UIView alloc] init] autorelease];
UITableView *tableView = [[[UITableView alloc] init] autorelease];
tableView.dataSource = self;
tableView.delegate = self;
[view addSubview:tableView];
self.view = view;
}
Your view controller should either inherit UITableViewController or implement the UITableViewDataSource and UITableViewDelegate protocols.
You have to actually specify the layout of your different views, not just add them as subviews. Try reading a tutorial about using AutoLayout programmatically.
You also want to set up all your views in loadView. There you can set the bottom of your extra view to the top of your table view.
- (void)viewDidLoad {
[super viewDidLoad];
UIView *view = [[[UIView alloc] initwithFrame:CGRectMake(set your frame)] autorelease];
UITableView *tableView = [[[UITableView alloc] initwithFrame:CGRectMake(set your frame)] autorelease];
tableView.dataSource = self;
tableView.delegate = self;
[view addSubview:tableView];
}
if you are using **ARC** remove autorelease.

Why does my TTTableViewController not appear

I have an application which contains a scrollview with two subviews (to scroll left and right between them)
Both views appear correctly and scrolling between the views worked fine. Now I want to change the first view to be a TTTableView (courtesy of Three20) but when I use the class 'TableControlsTestController' from the TTCatalog application all I see is an empty tableView
Note that this class contains all the data to display 6 cells
To add the new TTTableView I use the following
[scrollView addSubview:detailView.view];
where detailView is the instance of TableControlsTestController
To try and narrow down where the problem is I also tried calling
[self presentModalViewController:detailView animated:YES];
This correctly displays the tableview with the 6 cells.
Why when I try to add the view to the scrollView does this not work as I expect?
For reference if you do not have access to the TTCatalog
#import "TableControlsTestController.h"
///////////////////////////////////////////////////////////////////////////////////////////////////
#implementation TableControlsTestController
///////////////////////////////////////////////////////////////////////////////////////////////////
// NSObject
- (id)init {
if (self = [super init]) {
self.tableViewStyle = UITableViewStyleGrouped;
self.autoresizesForKeyboard = YES;
self.variableHeightRows = YES;
UITextField* textField = [[[UITextField alloc] init] autorelease];
textField.placeholder = #"UITextField";
textField.font = TTSTYLEVAR(font);
UITextField* textField2 = [[[UITextField alloc] init] autorelease];
textField2.font = TTSTYLEVAR(font);
textField2.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
TTTableControlItem* textFieldItem = [TTTableControlItem itemWithCaption:#"TTTableControlItem"
control:textField2];
UITextView* textView = [[[UITextView alloc] init] autorelease];
textView.text = #"UITextView";
textView.font = TTSTYLEVAR(font);
TTTextEditor* editor = [[[TTTextEditor alloc] init] autorelease];
editor.font = TTSTYLEVAR(font);
editor.backgroundColor = TTSTYLEVAR(backgroundColor);
editor.autoresizesToText = NO;
editor.minNumberOfLines = 3;
editor.placeholder = #"TTTextEditor";
UISwitch* switchy = [[[UISwitch alloc] init] autorelease];
TTTableControlItem* switchItem = [TTTableControlItem itemWithCaption:#"UISwitch" control:switchy];
UISlider* slider = [[[UISlider alloc] init] autorelease];
TTTableControlItem* sliderItem = [TTTableControlItem itemWithCaption:#"UISlider" control:slider];
self.dataSource = [TTListDataSource dataSourceWithObjects:
textField,
editor,
textView,
textFieldItem,
switchItem,
sliderItem,
nil];
}
return self;
}
#end
It turns out that some of the events were not being propagated through the scrollView and therefore not handled in the TTTableViewController. To fix this I had to do the following in my scrollView
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[myNewTTViewController viewWillAppear:NO];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
[myNewTTViewController viewDidAppear:NO];
}
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
[myNewTTViewController viewWillDisappear:NO];
}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidDisappear:animated];
[myNewTTViewController viewDidDisappear:NO];
}
As soon as I did this the table popped into life

viewing tableViewLines on background of custom UIView

I have a viewController that has a tableView and a custom loading UIView that displays a loading message and spinner while the data of the tableView is being loaded.
The custom UIView has a red background that i can see, but i can still see the lines of the tableView, How can i get to display the custom uiview without seeing the tableView lines on the background.
#implementation LoadingView
#synthesize spinner;
- (id)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
// Initialization code
[self setUserInteractionEnabled:NO];
[self setBackgroundColor:[UIColor blueColor]];
// Spinner
spinner = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[spinner setCenter:CGPointMake(320/2, 150)];
[self addSubview:spinner];
[spinner startAnimating];
//title label
UILabel *titleLabel = [[UILabel alloc] init];
titleLabel.text = #"Loading...";
titleLabel.font = [UIFont boldSystemFontOfSize:18];
titleLabel.lineBreakMode = UILineBreakModeWordWrap;
titleLabel.backgroundColor = [UIColor clearColor];
titleLabel.textColor = [UIColor blackColor];
titleLabel.textAlignment = UITextAlignmentLeft;
CGFloat height = [titleLabel.text sizeWithFont: titleLabel.font constrainedToSize: CGSizeMake(300, 1500) lineBreakMode: UILineBreakModeWordWrap].height;
[titleLabel setFrame: CGRectMake(120, 180, 100, height)];
[self addSubview:titleLabel];
[titleLabel release];
}
return self;
}
- (void)drawRect:(CGRect)rect {
}
- (void)dealloc {
[super dealloc];
[spinner release];
}
#end
You can set the lines "off", when you start you loading process, setting them transparent.
// Setting transparent
tableView.separatorColor = [UIColor clearColor];
And when you remove the loading UIView, you change the color for the one that you want.
// Setting the desired color
tableView.separatorColor = [UIColor grayColor];
Another alternative is to set the table view hidden while you don't have data to be displayed, and than when the first or more row appear you place you set the table view to not hidden.
I hope this will help you!
Cheers,
VFN
The problem was that self.view in the viewController had the tableView assigned directly. The solution is to assign the self.view a UIView and add the tableView and the custom UIView on top of self.view.
This is sample code from the viewController.
- (void)loadView {
UIView *aView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]];
//tableView
self.tableView = [[[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain] autorelease];
//set the rowHeight once for performance reasons.
//self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.rowHeight = 75;
self.tableView.backgroundColor = [UIColor clearColor];
self.tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.tableView.autoresizesSubviews = YES;
[aView addSubview:tableView];
//set the rowHeight once for performance reasons.
self.view = aView;
[aView release];
}
- (void)viewDidLoad {
[super viewDidLoad];
loadingView = [[LoadingView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
[self.view addSubview:loadingView];
//fetch productsArray
[self productListRequestStart];
}