Xcode and alertview with view controller - xcode

What i am trying to achieve is go to another view when my alert view button is clicked. My alert view is inside my loadingView, this alert view is called from another class called classA.
This is how it is called in classA.
[LoadingViewController showError];
This is the method in loadingView in loadingView class.
+ (void)showDestinationError{
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Error"
message:#"Error"
delegate:self
cancelButtonTitle:#"OK"
otherButtonTitles: nil];
alert.tag = DEST_ERR;
[alert show];
}
Button action
+ (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(alertView.tag = DEST_ERR){
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:[NSBundle mainBundle]];
UINavigationController *secondView = [storyboard instantiateViewControllerWithIdentifier:#"NavigationController"];
secondView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[secondView presentModalViewController:secondView animated:YES];
}
}
This gives me an error. 'Application tried to present modal view controller on itself. Presenting controller is UINavigationController:..
Note: my method is a '+'

The problem is this line:
[secondView presentModalViewController:secondView animated:YES];
A view controller can't present itself, and in this case doesn't even exist yet).
The most obvious way to fix this is to make your clickedButtonAtIndex an instance method since it needs to access information about the particular instance. You would then use this:
[self presentModalViewController:secondView animated:YES];
Otherwise, you need to get a reference to a view which can present your view controller. There are various ways to do this depending on how your app is setup which could include getting it from your app delegate or referencing the app window.

Related

make modally presentation from UISplitViewControllers primary view to detail view

I have a plus button in my primary view controller of a UISplitViewController and i want to present something modally in my detail view, just like apple does when adding a new contact in address book in iPad. I have tried everything but nothing. I managed to do it but when i am trying to embed my presented view controller into a UINavigation controller then my presented controller covers the full screen. Any suggestions? Here is my code:
UINavigationController *navController = [self.splitViewController.viewControllers lastObject];
DetailTableViewController *controller = (DetailTableViewController *)navController.topViewController;
controller.definesPresentationContext = YES;
controller.providesPresentationContextTransitionStyle = YES;
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
EditTableViewController *etvc = (EditTableViewController *)[storyboard instantiateViewControllerWithIdentifier:#"EditTableViewController"];
UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:etvc];
etvc.patient = patient;
if (IDIOM == IPAD)
{
etvc.modalPresentationStyle = UIModalPresentationCurrentContext;
[controller presentViewController:nav animated:YES completion:nil];
} else {
[self presentViewController:nav animated:YES completion:nil];
}
I just successfully solved this problem by creating a custom segue whose implementation is:
- (void)perform
{
UIViewController *ctrl = self.sourceViewController;
UIViewController *dest = self.destinationViewController;
dest.modalPresentationStyle = UIModalPresentationCurrentContext;
[ctrl presentViewController:dest animated:YES completion:nil];
}
I'm seeing the behavior I want by invoking this segue from my detail view controller on the modal view I want to overlay it.
I think where your code is going haywire is here:
etvc.modalPresentationStyle = UIModalPresentationCurrentContext;
I think it should be:
nav.modalPresentationStyle = UIModalPresentationCurrentContext;
Though I haven't tested it.
Note that the Apple docs suggest that modalPresentationStyle is ignored on the iPhone (or on "horizontally compact devices"), so your "IS_IPAD" check may be redundant.
Hope this helps!

Usernotes in ios

I want to create a user note form in my application,currently am using one textview inside a view its looking bad !! is there any other control suits for this purpose? Main aim is when user click the button a small textview will appear they can add comments there and save it into plist.
I want something like this(check the image)
i want that kind of usernotes (its my image) please give me some advices and helps to develop this..
Using UIAlertView with UITextView can be useful for you.
Implement UIAlertViewDelegate in .h file.
UITextView *comments;
-(IBAction)btnAddClicked:(id)sender
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Noreply Email" message:#"\n\n\n\n\n" delegate:self cancelButtonTitle:#"Close" otherButtonTitles:#"Send", nil];
comments = [[UITextView alloc] initWithFrame:CGRectMake(15,45, 255, 100)];
[comments setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:#"mailbody.png"]]];
[comments setFont:[UIFont fontWithName:#"Helvetica" size:15]];
comments.scrollEnabled = YES;
[comments becomeFirstResponder];
[alert addSubview:comments];
[alert show];
[alert release];
}
Here alert will be prompted with small textview you can add comments and then handle text inside delegate method like this.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
if(buttonIndex==1) //Send button pressed
{
//handle your comment here
NSLog(#"%#",comments.text);
}
}

How to pass variable from uitableview to second tableview using didSelectRowAtIndexPath?

i want to pass the variable from a view controller to another one, so that when the user selects a certain row in the first table view, the application will take him to another view controller in which the details of the selected item will appear.
This is my code :
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *selectedAuthors = [theauthors objectAtIndex:indexPath.row];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard" bundle:nil];
Details *dvController = [storyboard instantiateViewControllerWithIdentifier:#"Details"]; //Or whatever identifier you have defined in your storyboard
dvController.selectedAuthors = selectedAuthors;
UIAlertView *messageAlert = [[UIAlertView alloc]
initWithTitle:#"Row Selected" message:authorNAme delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
// Display Alert Message
authorNAme = [[NSString stringWithFormat:[theauthors objectAtIndex:indexPath.row]] intValue];
[messageAlert show];
[self.navigationController pushViewController:dvController animated:YES];
}
selectedAuthors is a string
authorName is a global variable in which i want to store the content of the selected row.
Any help will be highly appreciated.
I see you are using storyboards.
In this case, to send information to the next view controller you have to implement
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
This method gets called every time a segue is about to be performed.
For instance, in didSelectRowAtIndexPath you can save the row's information in a dictionnary and pass it to the next view controller in the prepareForSegue method.
For detailed information you can check out Apple's sample project named SimpleDrillDown. It does the same thing you need to do:
http://developer.apple.com/library/ios/#samplecode/SimpleDrillDown/Introduction/Intro.html
NSString *titleString = [[[NSString alloc] initWithFormat:#"Element number : %d",indexPath.row] autorelease];
This is the simplest function used to send variables and it just came out of nowhere! For anyone lost this is the best function!!

UIAlertview is called multiple times

I have the following code in my application. On a view controller I have two UIButton controls that each do different operations. When I press the first button I have a UIAlertView to confirm the operation. This works fine. I setup the second button the same way. When I press the second button the first UIAlertView appears briefly, and then the second UIAlertView appears. It works okay at that point but then the first UIAlertView appears again.
If I take out the UIAlertViews completely and just update a label on the view to indicate which button was pressed I don't get either button called a second time so I have isolated this to the inclusion of the UIAlertViews.
Can anyone point to something in my code that is causing this? Here's the code.
- (IBAction)clearInspectionsClicked {
UIAlertView *alertClear = [[UIAlertView alloc] initWithTitle:#"Please Confirm"
message:#"Clear out all inspection data?"
delegate:self
cancelButtonTitle:#"Clear"
otherButtonTitles:#"Cancel", nil];
[alertClear show];
}
- (IBAction)loadSampleDataClicked {
UIAlertView *alertLoad = [[UIAlertView alloc] initWithTitle:#"Please Confirm"
message:#"Load Sample data?"
delegate:self
cancelButtonTitle:#"Load"
otherButtonTitles:#"Cancel", nil];
[alertLoad show];
}
-(void) alertView:(UIAlertView*)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString *title = [alertView buttonTitleAtIndex:buttonIndex];
if ([title isEqualToString:#"Clear"])
{
[self clearInspections];
[self.StatusLabel setText:#"Inspection data has been cleared!"];
}
if ([title isEqualToString:#"Load"])
{
[self loadSampleData];
[self.StatusLabel setText:#"Sample data has been loaded!"];
}
}
Is it possible that you have one of the buttons wired to two of those actions? it is possible to wire multiple actions to one given control in Interface Builder, and it would cause this exact behavior.

UITabBarController + UINavigationController problem xcode project

I have a problem, I have created a project window based application in xcode, then I create a UITabBarController that manages two views all programmatically, the second view is a tableView and I want to see in the top a UINavigationController, I have tried a lot but I don't know how to have a UINavigationController in the second view. this is the code:
ProjectAppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Creo una tabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//Create the two view controllers
UIViewController *vc1 = [[Visuale1ViewController alloc] init];
UIViewController *vc2 = [[Visuale2ViewController alloc] init];
//Make an array containing the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, vc2, nil];
//The viewControllers array retains vc1 and vc2, we can release
//our ownership of them in this method
[vc1 release];
[vc2 release];
//Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers];
//Setto la tabBarController come rootViewController di window
[window setRootViewController:tabBarController];
//The window retain tabBarController, possiamo lasciare il nostro riferimento
[tabBarController release];
[self.window makeKeyAndVisible];
return YES;
}
Visuale1ViewController.h
#implementation Visuale1ViewController
- (id)init{
[super initWithNibName:#"Visuale1ViewController" bundle:nil];
//Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//Give it a label
[tbi setTitle:#"Visuale 1"];
return self;
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle {
return [self init];
}
Visuale2ViewController.h
#implementation AnswerViewController
- (id)init{
//Call the superclass's designated initializer
/*[super initWithNibName:nil
bundle:nil];*/
[super initWithStyle:UITableViewStyleGrouped];
answers = [[NSMutableArray alloc] init];
for (int i = 0; i<10 ; i++) {
[answers addObject:[Answer DefaultAnswer]];
}
//Get the tab bar item
UITabBarItem *tbi = [self tabBarItem];
//Give it a laber
[tbi setTitle:#"Visuale 2"];
return self;
}
- (id)initWithStyle:(UITableViewStyle)style{
return [self init];
}
//All below are all methods to work the table view, and all go well, the only problem it's the UINavigationController, to manage then the detail of the table...
Now I want to know how I can put a UINavigationController in the second view. I try do this, in ProjectAppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
//Creo una tabBarController
UITabBarController *tabBarController = [[UITabBarController alloc] init];
//Create the two view controllers
UIViewController *vc1 = [[Visuale1ViewController alloc] init];
UIViewController *vc2 = [[Visuale2ViewController alloc] init];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:vc2];
//Make an array containing the two view controllers
NSArray *viewControllers = [NSArray arrayWithObjects:vc1, navController, nil];
//The viewControllers array retains vc1 and vc2, we can release
//our ownership of them in this method
[vc1 release];
[vc2 release];
//Attach them to the tab bar controller
[tabBarController setViewControllers:viewControllers];
//Setto la tabBarController come rootViewController di window
[window setRootViewController:tabBarController];
}
In this way I can visualize the NavigationBar, but I lost the name of the SecondTabBar. Sorry for my english, how I can do this?
Yes in the second view you have to set title as
[self.navigationItem setTitle:#"Visuale2"];
For TabBar title-
UITabBar *tabBar = [self.tabBarController tabBar];
NSArray *tabBarItems = [tabBar items];
UITabBarItem *secondTabBarItem = [tabBarItems objectAtIndex:1];
[secondTabBarItem setTitle:#"Visuale2"];
I left the code as it was and I added in init Visale2ViewController this:
UIImage *i = [UIImage imageNamed:#"Hypno.png"];
[tbi setImage:i];
and now in can see the text Visuale2 in tabBar and the image...i don't know why...
You need to set the tabBarItem property for your UINavigationController. Something like this.
UITabBarItem *tabItem = [[UITabBarItem alloc] initWithTitle:#"Visuale 2" image:nil tag:1];
UIViewController *vc2 = [[Visuale2ViewController alloc] init];
navController = [[UINavigationController alloc] initWithRootViewController:vc2];
navController.tabBarItem = tabItem;
After looking at it,the scenario seems to be same like me.What I faced for the first time when doing Tab+Navigation.
I am sure that there is some problem with your Tab+Navigation based application. Although it shows the Tab as well as navigation are not able to navigate the basic flow.And it is very difficult to solve your problem with such less code.
Instead of this, I had an alternate solution for the same:
Once you have a tab bar in a XIB, the easiest way to approach this is to drag a UINavigationController object over from the Library window (looks like a left nav bar button on a gold background) into the Tree View for your tab bar (the text only view, not the GUI). Place it under the tab bar, then drag your existing view controller under the tab bar controller instead of under the tab bar.
When you go to view that tab you should then see a navigation bar on the top of it... if you are loading the navigation controller from another xib, you'll modify the nav bar in the tab bar xib.
else you can below you can follow the best url for the same:
http://books.google.co.in/books?id=2yYlm_2ktFYC&pg=PA179&lpg=PA179&dq=navigation+with+the+tab+based+application+iphoneSDK&source=bl&ots=nf2YYjX5Am&sig=COpHj9wOtsDChQBglpsljSTsElw&hl=en&ei=3ZoFTeGSOI_tsgbc_Iz6CQ&sa=X&oi=book_result&ct=result&resnum=6&ved=0CDAQ6AEwBQ#v=onepage&q&f=false
http://www.youtube.com/watch?v=LBnPfAtswgw
Hope this will surely solve your problem.

Resources