How to customize TTPostController action button - iphone

Three20 TTPostController has default "Done" button for action. How do I customize the button to e.g "Save"

#import "CustomTTPostController.h"
#implementation CustomTTPostController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.navigationItem.rightBarButtonItem.title = #"Save";
}
return self;
}
#end

Related

How and where self object is alloc?

There are many methods which override functions like this:
- (void)viewDidLoad
{
[super viewDidLoad];
}
So the super is calling the parent classes function, but where is self allocated?
self is a pointer to the “current object”, it’s allocated in the usual initialization formula:
NSObject *foo = [[NSObject alloc] init];
When you call [NSObject alloc], the class creates an object that you later refer to as self.
Basically it is initialized in init function like this:
- (id) init
{
self = [super init];
return self;
}
Whenever we initialize our class like this with nib:
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
or simply init the self will be allocated
Here
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
This is assigning value in self
When you call you class and alloc it then it access it's super class init method and pass to self.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self)
{
}
return self;
}

Connecting Touch Up Inside event to File's Owner

I’m working through the Multiview Applications chapter of Apress’s Beginning iOS5 development and ran into an issue making a connection from a button on one of the views to File Owner. The general premise of the program is to use a root controller, BIDSwitchViewController to switch between two content views BIDBlueViewController and BIDYellowViewController. Each of the content views has a single button connecting the Touch Up Inside event to the File’s Owner. I had no problem making the connection in BIDBlueViewController, but when I attempt to make this connection in the BIDYellowViewController I’m unable to make a connection between the Touch Up Inside event and the File’s Owner icon.
The instructions in the book state
Drag a Round Rect Button from the library over to the view, using the
guidelines to center the button in the view, both vertically and
horizontally. Double-click the button, and change its title to Press
Me. Next, with the button still selected, switch to the connections
inspector drag from the Touch Up Inside event to the
File's Owner icon, and connect to the blueButtonPressed action method
I’m able to do this without any issues, however when I try complete this instruction for the Yellow View
Drag a Round Rect Button from the library over to the view, using the
guidelines to center the button in the view, both vertically and
horizontally. Double-click the button, and change its title to Press
Me. Next, with the button still selected, switch to the connections
inspector drag from the Touch Up Inside event to the
File's Owner icon, and connect to the yellowButtonPressed action method
The File Owner icon will not let me make the connection, as I drag from the Touch Up Inside event to the File Owner icon File Owner never highlights and I cannot make the connection.
Here is my code
The root controller interface
//
// BIDSwitchViewController.h
// ViewSwitcher
//
//
#import <UIKit/UIKit.h>
#class BIDYellowViewController;
#class BIDBlueViewController;
#interface BIDSwitchViewController : UIViewController
#property (strong, nonatomic) BIDYellowViewController *yellowViewController;
#property (strong, nonatomic) BIDBlueViewController *blueViewController;
- (IBAction)switchViews:(id)sender;
#end
The root controller implementation
//
// BIDSwitchViewController.m
// ViewSwitcher
//
#import "BIDSwitchViewController.h"
#import "BIDYellowViewController.h"
#import "BIDBlueViewController.h"
#implementation BIDSwitchViewController
#synthesize yellowViewController;
#synthesize blueViewController;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
self.blueViewController = [[BIDBlueViewController alloc]
initWithNibName:#"BlueView" bundle:nil];
[self.view insertSubview:self.blueViewController.view atIndex:0];
[super viewDidLoad];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
- (IBAction)switchViews:(id)sender{
if (self.yellowViewController.view.superview == nil){
if (self.yellowViewController == nil) {
self.yellowViewController =
[[BIDYellowViewController alloc] initWithNibName: #"YellowView"
bundle:nil];
}
[blueViewController.view removeFromSuperview];
[self.view insertSubview:self.yellowViewController.view atIndex:0];
} else {
if (self.blueViewController == nil){
self.blueViewController = [[BIDBlueViewController alloc] initWithNibName:#"BlueView"
bundle:nil];
}
[yellowViewController.view removeFromSuperview];
[self.view insertSubview:self.blueViewController.view atIndex: 0];
}
}
- (void) didReceiveMemoryWarning {
// Release the view if it doesn't have a super view
[super didReceiveMemoryWarning];
// Release any cached data, images, etc, that aren't in use
if (self.blueViewController.view.superview == nil){
self.blueViewController = nil;
} else {
self.yellowViewController = nil;
}
}
#end
The blue content view controller interface
//
// BIDSwitchViewController.h
// ViewSwitcher
//
#import <UIKit/UIKit.h>
#class BIDYellowViewController;
#class BIDBlueViewController;
#interface BIDSwitchViewController : UIViewController
#property (strong, nonatomic) BIDYellowViewController *yellowViewController;
#property (strong, nonatomic) BIDBlueViewController *blueViewController;
- (IBAction)switchViews:(id)sender;
#end
The blue content view controller implementation
//
// BIDBlueViewController.m
// ViewSwitcher
//
//
#import "BIDBlueViewController.h"
#interface BIDBlueViewController ()
#end
#implementation BIDBlueViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
The yellow content view controller interface
//
// BIDYellowViewController.h
// ViewSwitcher
//
#import <UIKit/UIKit.h>
#interface BIDYellowViewController : UIViewController
- (IBAction)yellowButtonPressed;
#end
The yellow content view controller implementation
//
// BIDYellowViewController.m
// ViewSwitcher
//
//
#import "BIDYellowViewController.h"
#interface BIDYellowViewController ()
#end
#implementation BIDYellowViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end

Call [tableView realoadData] on RootViewController from AppDelegate?

In my app I am making an HTTP request and waiting and updating my RootViewController with the data. The problem is, I need to make a call to [tableView reloadData] after my data collection has finished, as it stands right now the tableview will either be partially populated or not populated at all. I came across this thread:
Passing an NSDictionary to a ViewController from AppDelegate
Which included this solution for setting data in the view conroller from app delegate, but I am wondering how I can adapt this for my situation? All I need, I think, is a reference to the rootViewController in appDelegate so I can call reloadData when I need to
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil tableDataSource:(NSArray*)tableData {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
tableDataArray = [tableData retain];
}
return self;
}
Thanks for any ideas
Declare that tableView as a property such as myTableView in that class. Now reference that class and call [classObject.myTableView reloadData];
It should work.
You can give the appDelegate a property that receives a pointer to the RootViewController and which is set by the RootViewController when it is initialized.
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
tableDataSource:(NSArray*)tableData
{
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
{
// Custom initialization
tableDataArray = [tableData retain];
(MyAppDelegate *)([[UIApplication sharedApplication] delegate]).rootController = self;
}
return self;
}

removing warnings caused by custom UITableViewCell class

I am getting the following warnings in the code:
Should I just remove all of these methods in the code or what?
#import "ReservationCell.h"
#implementation ReservationCell
#synthesize origin;
#synthesize destination;
#synthesize time_range;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)dealloc
{
[origin release];
[destination release];
[time_range release];
[super dealloc];
}
- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#pragma mark - View lifecycle
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
#end
Also I am getting this final linker warning, anyone know how to fix it?
All of those warnings are for UIViewController methods. A UITableViewCell is a UIView, so it would not respond wo any of those.

UIViewController with Nib File and Inheritance

I'm trying to do something really tricky and I'm still stuck at a point. I'm attempting to instance an UIViewController with a Nib file inherited from an other UIViewController with an other Nib file.
The problem is when I instantiate my son UIViewController .
// SonViewController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:nibNameOrNil
bundle:nibBundleOrNil])) {
// Custom initialization.
}
return self;
}
The init method initWithNibName:bundle: should call the super class but it only call its own nib file. In the super class, I tried to override the initWithNibName:bundle: method and put the nibName myself like that :
// MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil
bundle:(NSBundle *)nibBundleOrNil
{
if ((self = [super initWithNibName:#"MotherViewController"
bundle:nibBundleOrNil])) {
// Custom initialization.
}
return self;
}
It only init and display the Mother Class and its IB Object. I understand why but I begin thinking it is impossible to do what I want. Any suggestion ?
Edit:
I would use my SonViewController just like that :
SonViewController *son = [[SonViewController alloc]
initWithNibName:#"SonViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:son animated:YES];
[son release];
It should display son and mother IB Object...
Regards,
kl94
Normally, you should only use a specific nib in the init method, and not the initWithNibName:bundle:, for this reason.
#implementation MotherViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
//custom initialization
}
return self;
}
- (id)init {
return [self initWithNibName:#"MotherViewController" bundle:nil];
}
Then, to use the default nib for MotherViewController, you just use [[MotherViewController alloc] init];.
As an alternate, you could define a different initializer in MotherViewController for this reason.
#implementation MotherViewController
- (id)_initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
//custom initialization
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
return [self _initWithNibName:#"MotherViewController" bundle:nibBundleOrNil];
}
Then, use a private category interface to tell SonViewController about this method.
//SonViewController.m
#interface MotherViewController (PrivateInit)
- (id)_initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil;
#end
#implementation SonViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
//custom initialization
}
return self;
}
I know this is an old thread, but I just found an incredibly blog post here.
Essentially, you have to iterate through all the views of the parent class and add them as subviews to your child class. Here's how I implemented it in my project:
// ChildViewController.m
- (void)viewDidLoad
{
[super viewDidLoad];
[self addSubviewsFromSuperclass];
}
// ParentViewController.h
- (void)addSubviewsFromSuperclass;
// ParentViewController.m
- (void)addSubviewsFromSuperclass
{
UIView *selfView = self.view;
UIView *nibView = nil;
#try
{
nibView = [NSBundle.mainBundle loadNibNamed:NSStringFromClass([self superclass]) owner:self options:nil][0];
}
#catch (NSException *exception)
{
NSLog(#"Something exceptional happened while loading nib:\n%#", exception);
}
self.view = selfView;
for (UIView *view in nibView.subviews)
{
[self.view addSubview:view];
}
}
That addSuviewsFromSuperclass method is not my coding. I have to give full credit to the author of the blogpost I mentioned above. Download his example project and you'll find it in his JMViewController.m.