I want to push a view controller from my FirstViewController to load BookDetailsViewController. here's the setup I currently have.
// FirstViewController:(this is inside a uinavigationcontroller)
-(IBAction)viewBookDetails:(id)sender
{
NSLog(#"woo");
BookDetailsViewController *bdvc = [[BookDetailsViewController alloc] init];
[self.navigationController pushViewController:bdvc animated:YES];
}
==============================================================
// BookScrollViewController: (where the button is located)
[book1UIButton addTarget:self action:#selector(viewBookDetails:)
forControlEvents:UIControlEventTouchUpInside];
-(void)viewBookDetails:(id) sender
{
FirstViewController *fvc = [[FirstViewController alloc] init];
[fvc viewBookDetails:sender];
}
==============================================================
//how BookScrollViewController is created
BookScrollViewController *controller = [bookViewControllersArray
objectAtIndex:page];
if ((NSNull *)controller == [NSNull null]) {
NSString *bookTitle = #"ngee";
controller = [[BookScrollViewController alloc]initWithBook:bookTitle
imageNamesArray:imgDataArray pageNumber:page totalResult:[newReleasesArray
count]];
[bookViewControllersArray replaceObjectAtIndex:page withObject:controller];
[controller release];
}
// add the controller's view to the scroll view
if (nil == controller.view.superview) {
CGRect frame = bookScroll.frame;
frame.origin.x = frame.size.width * page;
frame.origin.y = 0;
controller.view.frame = frame;
[bookScroll addSubview:controller.view];
}
when I tap the button in BookScrollViewController it calls the IBAction I have in FirstViewController coz it prints my nslog but it's not loading pushing the BookDetailsViewController to the navigation stack.
I tried assigning a button from FirstViewController to call the IBAction and it loads just fine. So, how can I successfully call the IBAction from FirstViewController using the button from BookScrollViewController?
thanks!
When you are assigning an action in the following way:
[book1UIButton addTarget:self action:#selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
You say that (current object: self) BookScrollViewController *self will respond to events UIControlEventTouchUpInside of book1UIButton. That means when user tap on button method viewBookDetails of object of class BookScrollViewController will be called.
As you mentioned in your question you have defined and implemented such method in FirstViewController.
Now you should
implement that method in BookScrollViewController that will push new controller onto navigation stack, or
set another object that will respond on that button event, that object can be of class FirstViewController, for example, [book1UIButton addTarget:self.firstViewController action:#selector(viewBookDetails:) forControlEvents:UIControlEventTouchUpInside];
When you do this:
FirstViewController *fvc = [[FirstViewController alloc] init];
you obviously create a new instance of FirstViewController. That new instance is not in a UINavigationController, so you can't push a new viewController onto its navigationController property.
What you probably want to do is reference the existing instance of FirstViewController and call the method on it instead of creating a new instance of it.
Related
I have a UIButton in UIVIewController and I need to push it to the UIVIew when I press the button.But this gives me the warning of incompatible pointer type sending
how to do this
What I am doing this:
-(void)press{
displayView *disp=[[displayView alloc]init];
[self presentModalViewController:disp animated:No];
}
this gives me warning and crashes my application.
presentModalViewController accepts a UIViewController instance (not UIView). If you want to display a particular view, place it within a view controller first.
UIViewController *viewController = [[UIViewController alloc] init];
DisplayView *displayView = [[DisplayView alloc] init];
[viewController.view addSubview: displayView];
[self presentModalViewController:viewController animated:NO];
I would like to know a method for navigating from one page to another page onclick .I
have tried
[self.navigationController
pushViewController:self
.objectNewlist
animated:YES];
but it is showing the error
Incompatible pointer types sending 'newlistplist *' to parameter of type 'UIViewController *'
my main class file is a subclass of UIViewController
and the second class is a subclass of UITableViewController
can any one help me out with this problem?
You can use
UIViewController *yourViewController = [[YourViewControllerClass alloc] initWithNibName:#"<name of xib>" bundle:nil];
[self presentModalViewController:yourViewController animated:YES];
[yourViewController release];
In case the new view is also to be created programmatically, you can do that in the viewDidLoad method of YourViewControllerClass and change the initialization to
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
In YourViewController when you wish to come back to previous view on some button action you can use
[self dismissModalViewControllerAnimated:YES];
Another way that you can do is
UIViewController *yourViewController = [[YourViewControllerClass alloc] init];
[self addSubview:[yourViewController view]];
and to remove the view you can use
[self.view removeFromSuperview];
-(void)onClickButton
{
ViewControllerClass *objViewControllerClass = [ViewControllerClass alloc] init] ;
[self presentModalViewController:objViewControllerClass animated:YES];
[objViewControllerClass release];
}
objViewControllerClass (object of another class)
I want to build Grouped View in Modal View which is destination state from "+" button on the Navigation bar in main table view.
I have written this code:
-(void)viewDidLoad
{
//title
self.title = #"Set";
//addBtn
UIBarButtonItem *addBtn = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:#selector(toggleEditing)];
self.navigationItem.rightBarButtonItem = addBtn;
[addBtn release];
}
-(IBAction)toggleEditing
{
}
create a subclass of UITableViewController named MyTableViewController
use presentModalViewController:animated: in your current viewController
-(IBAction)toggleEditing {
MyTableViewController *tableViewController = [[[MyTableViewController alloc] initWithStyle:UITableViewStyleGrouped] autorelease];
[self presentModalViewController:tableViewController animated:YES];
}
And you will need a delegate method that tells the viewController from where you showed the modal viewcontroller that a row was selected.
I have a root view controller which has few buttons. On click of these button, I shown up a tab bar controller with each controller showing the table view controller. I have achieved this. So basically, for each table view controller I have a dedicated class. To go ahead, I replaced the tab bar controller, and added the segmented controller to the navigation title view.
The question is how can I set the view based on the selected index. I am able to set the navigation title to be segmented control but on select I am unable to set the view.
Below is what i have achieved so far.
Note: What matters is a running code, I would do that code optimization later on. I dont want to hidde views. I want to call different view controller class.
RootViewController class (on click of the button, i call the first view controller. So that I can set the segment controller:
-(IBAction) pushSegmentController: (id) sender
{
NSLog(#"My Location Button being clicked/touched") ;
FirstViewController *firstViewController = [[FirstViewController alloc] init] ;
[self.navigationController pushViewController:firstViewController animated:YES];
// Releae the view controllers
[firstViewController release];
}
IN FirstViewController class:
-(void)viewDidLoad {
[super viewDidLoad];
NSArray *viewControllers = [self segmentViewControllers];
self.segmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"Table1", #"Table2"]];
self.navigationItem.titleView = segmentedControl;
[self.segmentedControl addTarget:self
action:#selector(indexDidChangeForSegmentedControl:)
forControlEvents:UIControlEventValueChanged];
self.segmentedControl.selectedSegmentIndex = 0;
}
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
NSUInteger index = aSegmentedControl.selectedSegmentIndex;
if(index ==0) {
UIViewController *table1Controller = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
**???? HOW SHOULD I SET THE VIEW OVER HERE... AS ITS A PART OF THE NAVIGATION CONTROLLER**
}
else { }
}
Note: I have tried using this option:
[navigationController setViewControllers:theViewControllers animated:NO];
But this option doesnt give me the right result. How should I go ahead with the same as I want to call a view controller class and set its view based on the selected index.
You probably don't want to have one view controller with different views depending on the button index, especially since you already have view controllers for your different screens.
If you want the table view controller to be pushed onto your navigation controller, so it will have a back button that gets you back to FirstViewController, use
-(void)indexDidChangeForSegmentedControl:(UISegmentedControl *)aSegmentedControl {
NSUInteger index = aSegmentedControl.selectedSegmentIndex;
UIViewController *newViewController = nil;
if(index ==0) {
newViewController = [[AustraliaViewController alloc] initWithStyle:UITableViewStylePlain];
} else {
newViewController = [[YourOtherViewController alloc] initWithStyle:UITableViewStylePlain];
}
[self.navigationController pushViewController:newViewController animated:YES];
}
If you'd rather have it slide in from the bottom and you want to handle setting up all necessary user interface (e.g. a dismiss button), replace that last line with
[self presentModalViewController:newViewController animated:YES];
What about?
self.view = table1Controller;
or
[self.view addSubview:table1Controller];
I just saw one more mistake you have done. You are allocating a UIViewController, but initializing it like a tableViewController(with initWithStyle).
If it's a subclass of UITableViewController, alloc it with that, not UIViewController.
I have a button that I'm creating in a UIViewController like so:
TOLoginButton* button = [[[TOLoginButton alloc] initWithFrame:CGRectMake(60,180,200,40)] autorelease];
[self.view addSubview:button];
The button is a UIControl and I'm trying to call another UIViewController from within the touchUpInside method, below is what I have but it doesn't work:
- (void)touchUpInside {
MyViewController *viewController = [[MyViewController alloc] init];
[super.view addSubview:viewController.view ];
}
I'm basically trying to call this viewController once my button is pressed.
So viewController1 has the button on it and once the button is pressed I want to do something like [viewController1.view addSubview:viewController2.view].
assuming the new UIViewController is full-screen, the more usual thing to do is
[self presentModalViewController:viewController animated:YES];
or if you're using a navigation controller
[self.navigationController pushViewController:viewController animated:YES];