in main view a have an navigationController on click on a row a call the detailview and add a button
seminareListinView.m
#import "SeminareListingView.h"
#import "Seminar.h"
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//gehe zurück zum ersten View
//NSLog(#"Received Data in seminareArray");
Seminar *dvController = [[Seminar alloc] initWithNibName:#"Seminar" bundle:nil];
NSString *selectedSeminarURL = [seminarURLArray objectAtIndex:indexPath.row];
//NSString *selectedNextXMLFile = [kategorienNextXMLFileArray objectAtIndex:indexPath.row];
dvController.seminarURLFromXML = selectedSeminarURL;
//dvController.XMLFile = selectedNextXMLFile;
[self.navigationController pushViewController:dvController animated:YES];
//Zeige den PDF Download Button
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"PDF Download" style:UIBarButtonItemStylePlain target:self action:#selector(showMenu)];
//anotherButton.action = #selector(showMenu);
dvController.navigationItem.rightBarButtonItem = anotherButton;
[anotherButton release];
[dvController release];
dvController = nil;
//[[self navigationController] popViewControllerAnimated:YES];
}
in the seminar view a have this method
seminar.m
- (void) showMenu
{
UIActionSheet *myMenu = [[UIActionSheet alloc]
initWithTitle: #"Überschrift"
delegate:self
cancelButtonTitle:#"Abbrechen"
destructiveButtonTitle:#"Etwas unwiderrufliches"
otherButtonTitles:#"Eins", #"Zwei", nil];
[myMenu showInView:self.view];
}
but i get an error by clicking on Button
2011-07-07 12:57:31.009 Seminar App2[4352:207] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[SeminareListingView showMenu]: unrecognized selector sent to instance 0x6305f90'
*** Call stack at first throw:
In
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"PDF Download" style:UIBarButtonItemStylePlain target:self action:#selector(showMenu)];
Cange to:
UIBarButtonItem *anotherButton = [[UIBarButtonItem alloc] initWithTitle:#"PDF Download" style:UIBarButtonItemStylePlain target:dvController action:#selector(showMenu)];
It looks like you're implementing the showMenu method in the Seminar class, but you're telling the bar button to call it on the SeminareListingView object. If that's the case, then you need to set the bar button's delegate to an instance of the Seminar class.
you are doing it wrong. Put your barButton ite code into viewDidLoad of Seminar. You are adding target self which denotes to current view of stack while you actually want to assign target to Seminar Controller. So please cut the code(Adding BarButtonItem) from cellForRowMethod to viewDidLoad of Seminar Controller.
Related
I'm having some strange difficulties with setting a title and adding a bar button to my navigation bar.
My app is a Tabbed application. The problem is within my first tab. The first tab consists of a UINavigationController, that contains a UITableViewController. From my AppDelegate:
UIViewController *firstViewController = [[MyTableViewController alloc] init];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:firstViewController];
_tabBarController = [[TFTabBarController alloc] init];
_tabBarController.delegate = self;
_tabBarController.viewControllers = [NSArray arrayWithObjects:navigationController, nil];
On selecting a row in firstViewController, I push a UIWebViewController on the navigation stack:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
UIViewController *secondViewController = [[MyWebViewController alloc] init];
[self.navigationController pushViewController:secondViewController animated:YES];
}
On intercepting the onclick of a hyperlink in the WebView, I push another UITableView on the navigation stack:
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
NSURL *url = [request URL];
if (navigationType == UIWebViewNavigationTypeLinkClicked)
{
UIViewController *thirdViewController = [[MyTableViewController alloc] init];
[self.navigationController pushViewController:thirdViewController animated:YES];
return NO;
}
return YES;
}
As thirdViewController opens, its navigation bar only contains the back button with the title of the previous view to navigate back. Its title and the right bar button are not shown...
This is how I try to display them:
self.title = #"My Title";
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Push me" style:UIBarButtonItemStylePlain target:self action:#selector(push)];
I also tried setting the button through the navigationController, but still no luck:
self.navigationController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:#"Push me" style:UIBarButtonItemStylePlain target:self action:#selector(push)];
What could I be doing wrong here?
Check that self.navigationItem and self.navigationController are not nil when accessed.
From the documentation:
The first time the property is accessed, the UINavigationItem object is created. Therefore, you shouldn’t access this property if you are not using a navigation controller to display the view controller.
A good place to access these members is under -viewDidLoad and not upon initialization.
I'd like to add a UINavigationController to my app info view (NOT to my main view). I've watched/read a number of tutorials showing how to add it to the main window through the AppDelegate using IB. In my case, I only want it to appear when a user presses the info button and is brought to the infoView. Here is how I switch to the infoView within my MainViewController:
- (IBAction)infoButtonPress:(id)sender
{
// Create pointer to instance of InfoViewController
InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
// Add view switching animation
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
// Change view using animation
[self presentModalViewController:infoView animated:YES];
}
At this point the infoView is displayed and I would like THIS to be the RootView of the UINavigationController. I have tried adding the line:
UINavigationController *infoNavController = [[UINavigationController alloc]
initWithRootViewController:infoView];
after creating an instance of InfoViewController, but the app crashes. Is it possible to add UINavigationController to views other than the main view?
Thanks.
You are very close with your implementation. Try it in this order.
InfoViewController *infoView = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
UINavigationController *infoNavController = [[UINavigationController alloc]
initWithRootViewController:infoView];
[infoView release]; // skip this if using ARC
infoView.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
[self presentModalViewController:infoNavController animated:YES];
UPDATE
To kill this modal, you will have to add your buttons to the main modal view.
InfoViewController.m
-(void)cancel:(id)sender {
[self dismissModalViewControllerAnimated:YES];
}
-(void)viewDidLoad {
[super viewDidLoad];
UIBarButtonItem *cancelButton =
[[UIBarButtonItem alloc] initWithTitle: #"Cancel"
style: UIBarButtonItemStylePlain
target: self
action: #selector(cancel:)];
self.navigationItem.leftBarButtonItem = cancelButton;
[cancelButton release];
}
-(void)reloadView{
NSLog(#"IN RELOAD VIEW ");
[[self tableView] reloadData];
}
- (void)viewDidLoad {
UIBarButtonItem * barButton = [[UIBarButtonItem alloc] initWithCustomView:activityIndicator];
[self navigationItem].rightBarButtonItem = barButton;
[activityIndicator startAnimating];
[self callStudentsWebService];
[super viewDidLoad];
self.clearsSelectionOnViewWillAppear = NO;
self.contentSizeForViewInPopover = CGSizeMake(320.0, 600.0);
[self setTitle:#"Students"];
self.navigationController.toolbarHidden = NO;
self.navigationController.toolbar.barStyle = UIBarStyleBlack;
UIBarButtonItem *add = [[UIBarButtonItem alloc] initWithTitle:#"Add Student" style:UIBarButtonItemStyleBordered target:self action:#selector(addStudent)];
UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *import = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemOrganize target:self action:#selector(addStudent)];
NSArray* toolbarItems = [NSArray arrayWithObjects:add,flexible,import, nil];
self.toolbarItems = toolbarItems;
}
Above is my Code in a Class which is a subclass of UITableViewController.
Now my problem is when I come onto the screen First Time it get the records from web service from the method [self callStudentsWebService] . Now i have a button in uitoolbar in the same view which updates the list . I am calling the reloadView method to refresh the List but it calls the numberOfRowsInSection and viewDidLoad method but does not call cellForRowAtIndexPath method. But when i goto other screen and come back again it does refresh my List from start.
What could be the reason ?? Please help i am new in iPad Development. And i m doing all this in iPad.
Thanks alot
EDITED
#interface StudentsList : UITableViewController {
DetailViewController *detailViewController;
UIToolbar *toolbar;
UIActivityIndicatorView *activityIndicator;
}
May be your data.count is 0? If there is some data it should call cellForRowAtIndexPath after [[self tableView] reloadData];
Do the refresh code in another separate method -(void)refreshData then call this whenever you want such as in -viewDidLoad or on a button tap...
Try to call -(void)reloadView from
-(void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self reloadView];
}
I have a bug in my code and I cannot seem to find it! I've created a button called addButton programatically and set its selector to add—but the app crashes in the simulator every time the button is pressed. Here is my code.
-(void)viewDidLoad{
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:#selector(add:)];
self.navigationItem.rightBarButtonItem = addButton;
[addButton release];
}
and the code for the add button is :
- (IBAction)add
{
MyDetailViewController * detail = [[MyDetailViewController alloc]init];
detail.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentModalViewController:detail animated:YES];
//[detail.text becomeFirstResponder];
[detail release];
}
Thanks for the help guys :D
Your add selector has a colon at the end which means it is trying to use an add method with a parameter but your add method does not expect a parameter object. You need to remove the colon from your selector by changing your Bar Button Item allocation to this:
UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(add)];
How to set action to the backButtonItem on the navigation bar? I have a navigation bar, when I'm pressing the back button, I need to alert some message to the user, and only after user's reaction - return to the previous view. How can I do it? Thanx!
- (void)viewDidLoad
{
[super viewDidLoad];
//no one field don't changed yet
isDirty = FALSE;
//edited user
//set default values
newData = [data copy];
//setting navigation controller rigth button
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"Save"
style:UIBarButtonSystemItemDone
target: self
action: #selector(saveBtnUserClick)];
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:#"Back"
style:UIBarButtonSystemItemDone
target: self
action: #selector(backBtnUserClick)];
self.navigationItem.backBarButtonItem = leftButton;
[leftButton release];
}
//and my method for reaction
-(IBAction) backBtnUserClick
{
NSLog(#"\n Back pressed");
//back to previous view
[self.navigationController popViewControllerAnimated: TRUE];
}
Add the < UINavigationControllerDelegate > in the header file and use this in the .m
- (BOOL)navigationBar:(UINavigationBar *)navigationBar shouldPopItem:(UINavigationItem *)item
{
//insert your back button handling logic here
// let the pop happen
return YES;
}
This sounds like a job for UIAlertView. Instead of calling popViewControllerAnimated: in your IBAction methods, alloc/init a UIAlertView and present it. Then, when the user taps a button on the UIAlertView, dismiss the UIAlertView and call popViewControllerAnimated:.
- (IBAction)backBtnUserClicked:(id)object {
UIAlertView *av = [[[UIAlertView alloc] initWithMessage:#"Wait!"
delegate:self
cancelButtonTitle:#"Ok"
otherButtonTitles:nil] autorelease];
[av show];
}
In your UIAlertViewDelegate methods call popViewControllerAnimated:.
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
[[self navigationController] popViewControllerAnimated:YES];
}
To set the action on the back button:
[[[self navigationController] leftBarButtonItem] setTarget:self];
[[[self navigationController] leftBarButtonItem] setAction:#selector(backBtnUserClicked:)];