IBAction that create a new UIViewController that is identical to the current one - objective-c

I am developing a simple drawing app that saves the user's change (paint) then allow the user to have a new empty page when clicking on "New Page" button.
I'm facing a problem while trying to create a new page..
I'm using the single view template and currently my app contains only one ViewController. I want a new ViewController that is identical to the current one (but without the user's change (the user paint)) to be generated automatically when clicking on the "new page" button.
It is kind of similar to the notepad app, except that it doesn't use a tableView. I've looked over a lot of NotePad app source code, but I can't find what I actually need!
I hope you can help
thanks in advance

try this:
- (IBAction)newPagePressed
{
UIViewController *newVC = [[self class] alloc] init];
[self.navigationController setViewCntrollers:#[newVC]];
}
you'll need to use UINavigationController to achieve it, otherwise you can try:
- (IBAction)newPagePressed
{
MyAppDelegate *appDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
UIViewController *newVC = [[self class] alloc] init];
appDelegate.rootViewController = newVC;
}

Related

Using the segue from master detail application with custom button.

So, I have a master detail application. So far an object is created at launch and added to the master list, the detail view works fine as well.
What I would like to do, is to let the user add object to the master list by pressing a button. I don´t need a new view for that though, I want to use the standard Detail View for this part.
When the user taps the "Add" button, a new object should be created and then go into the detailed view of that object. I thought, "hey, why not use the standard segue as well?"
So I added a Bar Button Item to the MasterView.
This my code from MasterViewController.m:
-(IBAction)addNewItem:(UIStoryboardSegue *)segue{
if([[segue identifier] isEqualToString:#"showDetail"]){
CoolItem *newItem;
NSDate *today = [NSDate date];
newItem = [[CoolItem alloc]initWithDate:today];
[self.dataController addCoolItemWithItem:newItem];
DetailViewController *detailViewController = [segue destinationViewController];
detailViewController.coolItem = newItem;
}
}
"showDetail" is the identifier of the standard segue that comes with the master detail template.
The result: Nothing! The method is not called at button tap.
I´ve made sure my button is connected to this method, (it does however shows up as a unwind segue in the document outline).
Any ideas on what goes wrong?
I'm not sure what went wrong but I was able to achieve the desired functionality using the following code:
-(void)viewDidload {
...
self.navigationItem.rightBarButtonItem =
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:#selector(Add:)];
}
-(IBAction)Add:(id)sender
{
// Create the new item and set it as active
...
[self performSegueWithIdentifier: #"SegueName" sender:self];
}
Hope this helps
Yaron

Xcode storyboard how to detect touch item

I have a storyboard setup (the default) with the tabbed application template.
I want to add a BarItem and have that launch a URL when touched. I don't want it to go to a view in the app. How can I do this using the storyboard?
I am new the story board concept and not sure what to add to the story board to add a new bar item and then make it call a method.
First you need to put a function in the ViewController code something like...
- (IBAction)barButtonPressed:(id)sender
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
Then go add the button to the view controller in the Storyboard and ctrl click drag from the button to the function.
This will make the button run that function when pressed.
If you mean the Navigation bar
//in viewDidLoad or wherever
UIBarButtonItem *openWebLink = [[UIBarButtonItem alloc]
initWithTitle: #"OpenWebLink"
style: UIBarButtonItemStyleBordered
target: nil action: #selector(openLink)];
[self.navigationItem setBackBarButtonItem: openWebLink];
-(void) openLink
{
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"http://www.google.com"]];
}
If you don't, look at Fogmeister answer
Code not tested!

Instant / Autosave in IOS

For background: I'm a Windows automation and data translation "expert" (or so they say grins) in my day job. I've been dabbling with Objective-C coding off and on since I bought my first Mac in 2004.
I'm working on an IOS app. My data container class knows how to save and load from disc, and each object responds to an instance method of -(void)saveToImpliedFilename{} or -(void)save:(NSString *)filename {}. There's a static call to load the data files from storage and create distinct data objects from them (they're fairly lightweight objects, so I'm not worried about loading several at a time). The app's domain is such that many of them won't ever be loaded at once anyway.
+(NSArray *)loadData {}
That's all working fine and wonderful. In storage the objects are stored as Xml and life is good.
Where I'm having trouble is when trying to modify the tutorials so that two things happen for me:
Quick note: I'm using the tutorial as a basis for POC coding, then I'll go back and start over with the "real" coding, reusing my data objects and some of the other utility I've built along the way.
Here's my list of goals and the issues:
I want the table view to tell the data objects to save at pretty much every "edit" event. The only one I can consistently get to work is reorganizing the table's order. (the save button and adding a new entry works fine)
entering a new entry into the list creates a nice modal editor with a save and a cancel button which work wonderfully. But if I edit an existing entry, I can't reproduce the save buttons' behaviors. Each time I try, the buttons' events no longer fire. I can't figure out where I'm going wrong.
I'm using the "Editable Table View" project from this tutorial series as my basis: http://www.aboutobjects.com/community/iphone_development_tutorial/tutorial.html
In the following code, the [self isModal] test is where the save/cancel buttons are made visible and wired up. Bringing up the new-entry screen is apparently the only time it's modal. I tried wiring this stuff up so that the buttons were created all the time, but again, the events never fire for either one. The next block below is where the editable table view is called explicitly with the NEW functionality, but the nonModal view of the same tableview is called by the select event on the selector table.
So...
// code snipped for the new/modal editor
- (void)viewDidLoad {
// Uncomment the following line to preserve selection between presentations.
// self.clearsSelectionOnViewWillAppear = NO;
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
// If the user clicked the '+' button in the list view, we're
// creating a new entry rather than modifying an existing one, so
// we're in a modal nav controller. Modal nav controllers don't add
// a back button to the nav bar; instead we'll add Save and
// Cancel buttons.
//
if ([self isModal]) {
UIBarButtonItem *saveButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemSave
target:self
action:#selector(save)];
[[self navigationItem] setRightBarButtonItem:saveButton];
UIBarButtonItem *cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancel)];
[[self navigationItem] setLeftBarButtonItem:cancelButton];
}
// do stuff here to display my object...
}
// this code is called from the selection table to explicitly add a new data object.
- (void)add {
vhAddVehicleViewController *controller = [[vhAddVehicleViewController alloc] initWithStyle:UITableViewStyleGrouped];
id vehicle = [[Vehicle alloc] init];
[controller setVehicle:vehicle];
[controller setListcontroller:self];
UINavigationController *newNavController = [[UINavigationController alloc] initWithRootViewController:controller];
[[self navigationController] presentViewController:newNavController animated:YES completion:nil];
}
// this is where it's called on the table selection to show the same view without the save/cancel buttons.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
vhAddVehicleViewController *controller = [[vhAddVehicleViewController alloc] initWithStyle:UITableViewStyleGrouped];
NSUInteger index = [indexPath row];
id vehicle = [[self vehicles] objectAtIndex:index];
[controller setVehicle:vehicle];
[controller setTitle:[vehicle Vehiclename]];
[[self navigationController] pushViewController:controller animated:YES];
}
I'm assuming the issue is that presenting it makes it modal, where as pushing it doesn't...? That's fine. But when I take out the test for modal to try to keep the buttons working, no joy. The buttons draw and click when tapped, but the events don't fire.
HALP! :-)
Thanks much.
-- Chris (I logged in with my Google account so at the top of the page I'm showing as "user1820796") shrug
You forgot to call [super viewDidLoad];
Update
Try removing the cancel button that goes on the left side when pushing the view controller. See if save starts working. I think the problem is you should not add a left button to the navigation bar when the view controller is pushed.
Which method signature are you using?
- (void)save
{
NSLog(#"Saving");
}
Or
- (void)save:(id)sender
{
NSLog(#"Saving");
}
I still think this was related to push/popping the view rather than presenting the view. I switched it all to presentation and it's working how I want now.
Thanks for the assistance guys. Quite a different paradigm than I'm used to on the GUI stuff, but I'm getting there.
thanks!

Get title of selected tab in UITabBarController

I have set the title for all five of my tabs in my AppDelegate file. I would like to access that title in another view within my application. I have the code below to output to the log window the selected index of that selected tab but what I'd really like to get is the title for that tab. I did have a look through the UITabBarController Class Reference and I didn't see anything that would allow me to do this.
What I am trying to avoid is some sort of switch or if...else statement where I hardcode values I have already manually set in another file.
- (void)viewDidLoad {
[super viewDidLoad];
AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSLog(#"The currently selected tab has an index of: %d", appDelegate.tabBarController.selectedIndex);
}
This code works as intended. It would just be ideal to see the title.
UIViewController* vc = appDelegate.tabBarController.selectedViewController;
NSString* tabTitle = vc.tabBarItem.title;
If the code is in the selected view controller it's even easier:
NSString* tabTitle = self.tabBarItem.title;

Creating and Removing UIViews Programmatically

I am learning how to manipulate view displays programmatically, I manage to display a new view in my appDelegate with the following block of code:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *navController = [[UINavigationController alloc] init];
loginController = [[LoginController alloc] init];
[navController pushViewController:loginController animated:NO];
[self.window addSubview:navController.view];
[self.window makeKeyAndVisible];
return YES; }
I have added a button in this view that will remove this current view and supposedly programmatically display a new view, however, I only managed to remove the view and not display the new view.
my code to display the second view is the following:
HomeController *homeView = [[HomeController alloc] init];
[self.window addSubview:homeView.view];
[homeView.view release];
Please advise.. I've been searching for hours to no avail, using Switch Views Programmatically, iPhone Views, removeSuperview..
Basically I want to create a simple login flow, at app start I will display my first view (login form), after successful login I want to discard the old view and display the second view which is my home page.
You are on the right track with using the UINavigationController. In fact, you are almost there.
You already have two view controllers - one for the login page, and one for the home page. In the didFinishLaunchingWithOptions:, push both controllers onto the UINavigationController's stack: first the "home" controller, then the "login" one. Once the login controller detects that the login has been successful, call popViewControllerAnimated: or popToRootViewControllerAnimated: to get to the home page.