This is a view-based application
i have a button in the ViewController.m
Added a new subview controller class and trying to open the new view when the button is clicked.
Code :
- (IBAction) accessByProducts: (id) sender {
if(self.accessByProducts == nil) {
AccessByProducts *acb = [[AccessByProducts alloc] initWithNibName:#"AccessByProducts" bundle:[NSBundle mainBundle]];
self.accessByProducts = acb;
[acb release];
}
}
but it doesn't seem to open up . can anyone help me or guide me on this.
Thanks.
Try this:
if(self.accessByProducts == nil) {
AccessByProducts *acb = [[AccessByProducts alloc] initWithNibName:#"AccessByProducts" bundle:[NSBundle mainBundle]];
self.accessByProducts = acb;
[self.view addSubview:acb.view]; //add as sub view
[acb release];
}
Also, what type is your self.accessByProducts and does it do anything more than just keeping a reference?
Related
How I can do this:
If i press the "Einloggen" (Login) button
{
Show TabBarController with FirstViewController
}
(I'm new in this and i tried to find out something about that...
https://developer.apple.com/library/ios/featuredarticles/ViewControllerPGforiPhoneOS/Introduction/Introduction.html
Open another view when button is pressed in iOS)
The script/functions i have is following.
- (IBAction)loginAction:(UIButton *)sender {
self.view = self.tabBarController.view;
}
Then i got a black screen.
If i use the code from the stackoverflow question above, i get a error.
- (IBAction)loginAction:(UIButton *)sender {
UIViewController* tabViewController = [[UIViewController alloc] initWithNibName:#"tabViewController" bundle:[NSBundle mainBundle]];
[self.view addSubview:tabViewController.view];
}
Thank you in advance!
I have a "FairListViewController". This is the first ViewController. Here by touching a tableCell, I can go to next which is "MenuViewController". And there are two Button, "Product" for "ProductViewController" and "Events" for "EventViewController". I use "NavigationController" as a "rootViewController" in my appDelegate. Now when i touch "Product" button in "MenuViewController" in takes me to "ProductViewController" and touching "BackButton" it smoothly back to "MenuViewController". I did the same code for "ProductViewController", here it takes me to the "EventViewController" but after touching "BackButton", strangely it takes back to First Page which is "FairListViewController". I change the name of my IBAction Button name ( In "EventViewController" nib file) several times & reconnect with nib file, even delete the button from nib file and try with a new button, but the result is same! Can any one similar to this problem before. Any suggestion will be highly appreciable. Thanks a lot in Advance.
Here is my code :
IN MenuViewController (For ProductViewController Button):
- (IBAction)callProductGroups:(id)sender
{
ProductGroupViewController *productGroupViewController;
if(IS_IPHONE_5)
{
productGroupViewController = [[ProductGroupViewController alloc] initWithNibName:#"ProductGroupViewController" bundle:nil];
}
else
{
productGroupViewController = [[ProductGroupViewController alloc] initWithNibName:#"ProductGroupViewControlleriPhone4" bundle:nil];
}
productGroupViewController.topBarImageForAll = self.topBarImageForAll;
productGroupViewController.topBarButtonImageForAll = self.topBarButtonImageForAll;
productGroupViewController.buttonDividerImageForAll = self.buttonDividerImageForAll;
productGroupViewController.backgroundImageForAll = self.backgroundImageForAll;
productGroupViewController.backgroundImageiPhone4ForAll = self.backgroundImageiPhone4ForAll;
[self.navigationController pushViewController:productGroupViewController animated:YES];
}
In ProductViewController :
-(IBAction)goBack:(id)sender
{
[self.navigationController popViewControllerAnimated:YES];
}
In MenuViewController (For EventViewController Button):
- (IBAction)callEvents:(id)sender
{
EventViewController *eventViewController;
if (IS_IPHONE_5)
{
eventViewController=[[EventViewController alloc] initWithNibName:#"EventViewController" bundle:nil];
}
else
{
eventViewController=[[EventViewController alloc] initWithNibName:#"EventViewControlleriPhone4" bundle:nil];
}
eventViewController.topBarImageForAll = self.topBarImageForAll;
eventViewController.topBarButtonImageForAll = self.topBarButtonImageForAll;
eventViewController.buttonDividerImageForAll = self.buttonDividerImageForAll;
eventViewController.backgroundImageForAll = self.backgroundImageForAll;
eventViewController.backgroundImageiPhone4ForAll = self.backgroundImageiPhone4ForAll;
[self.navigationController pushViewController:eventViewController animated:YES];
}
In EvenViewController :
- (IBAction)backButton:(id)sender
{
[self.navigationController popToRootViewControllerAnimated:YES];
}
[There is no problem of Going to next ViewController, the problem is about Coming Back.]
Try instead of
[self.navigationController popToRootViewControllerAnimated:YES];
Use
[self.navigationController popViewControllerAnimated:YES];
I am new to iPhone programming. what I am trying is I have one screen with a button. And I want to change the view controller not only the view when I click that button (I know how to add subview) because from that 2nd view controler, I have to go to the third view which is not possible if I add subview in first place. Can anybody please help me with that? Is this possible? and if yes, how? All the views and view controller are created programmaticaly. I am not using IB.
EDIT: here is the relevant code that fires when clicking button
-(id)showCurrentLoc:(id)sender {
locationController = [currentLocController alloc];
[entry removeFromSuperview];
[newLoc removeFromSuperview];
[currentLoc removeFromSuperview];
[self.view setBackgroundColor:[UIColor clearColor]]; //[self.view addSubview: [locationController view]];
[self.navigationController pushViewController:locationController animated:YES]; [locationController release];
return 0;
} //Location Controller is the tableViewController
Thanks
Vik
You can do something like this
YourViewController *objYourViewController = [[YourViewController alloc] initWithNibName:#"YourViewController" bundle:nil];
[self.navigationController pushViewController:objYourViewController animated:YES];
[YourViewController release];
Usually, you use a navigation controller for this sort of thing so that the user can easily go back to the previous view. Your view controller would then do something like this:
[self.navigationController pushViewController:someNewViewController animated:YES];
If you want to manage the view controllers yourself, you can always just change the window's rootViewController property. Please read View Controller Programming Guide for the complete picture.
UINavigationController is what you need. It manages a stack of UIViewControllers and if you want to add new UIViewController just push it into this navigation stack. It automates back button behavior for you, and you can pop your current UIViewController from stack whenever you are done with it.
You could work with a UINavigationController. Adding your first UIViewController like this in the init method:
[self setViewControllers:[NSArray arrayWithObject:viewController]];
And then when a button is click or a choice is made, your push the second few controller with (in the first viewController):
[self.navigationController pushViewController:controller animated:YES];
This way you will also get an automatic (back button). Basicly you create a stack of UIViewControllers that you can push and pop like with a normal stack.
I hope this helps. Look into the following:
UINavigationController Class Reference
- (void) loadViewAtIndex:(NSInteger)index {
[self unloadViewAtIndex:activeViewIndex];
switch (index) {
case 1:
{
if (viewController1 == nil)
{
viewController1 = [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
}
viewController1.view.frame = CGRectMake(0.0, 0.0, viewController1.view.frame.size.width, viewController1.view.frame.size.height);
[window addSubview:viewController1.view];
}
break;
case 2:
{
if (viewController2 == nil)
{
viewController2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
}
viewController2.view.frame = CGRectMake(0.0, 0.0, viewController2.view.frame.size.width, viewController2.view.frame.size.height);
[window addSubview:viewController2.view];
}
break;
default:
break;
}
activeViewIndex = index;
}
- (void) unloadViewAtIndex:(NSInteger)index {
switch (index) {
case 1:
{
if (viewController1 != nil)
{
[viewController1.view removeFromSuperview];
[viewController1 release];
viewController1 = nil;
}
}
break;
case 2:
{
if (viewController2 != nil)
{
[viewController2.view removeFromSuperview];
[viewController2 release];
viewController2 = nil;
}
}
break;
default:
break;
}
}
I have a
UIViewController-> UINavigationBar + UITableView
Just a bit more explanation
I made it through UIBuilder..
1: Created a New UIViewController with XIB-file
2: Using UIBuiler i put a UINavigationController
3: Then i put UITableView underneath the navigationBar
so it gave me..
A: UIViewController-> UINavigationBar + UITableView
Now i am loading the data in UITableView from a Webservice which is working fine.
I again made a xib with sam config which is
B: UIViewController-> UINavigationBar + UITableView
So now when i try to push view B on view A using below code...it wont at all work...
SelectSiteViewController *siteViewController = [[SelectSiteViewController alloc] initWithNibName:#"SelectSiteViewController" bundle:nil];
[self.navigationController pushViewController:siteViewController animated:YES];
When i checked the UINavigationController *nav = self.navigation
nav is 0x0 that is i assume NIL.
Can anybody tell me whats wrong in here.. why is it nil...and how can i make it work..
Thanks a Lot....I would really appreciate any help
In UIBuilder verify that UINavigationController is referenced by the File's owner.
Got it.
I changed the Architecture a little bit.
I made a New UIViewController class with its xib. And coded new UINavigationController.
- (void)viewDidLoad {
[super viewDidLoad];
UINavigationController *navigationController
navigationController = [[UINavigationController alloc] init];
[self.view addSubview:navigationController.view];
switch (whichViewController) {
case 1:
viewController = [[xxxx alloc] init];
break;
case 2:
viewController = [[xxx1 alloc] init];
break;
default:
break;
}
[navigationController pushViewController:viewController animated:NO];
[viewController release];
}
And pushing the view in the Switch Statement....
I hope this makes sense ......
Thanks jamihash
So you are adding the tableview to the navigation controller right? This is how:
tableView = [[UITableViewController alloc] initWithStyle:UITableViewStylePlain];
navigationController = [[UINavigationController alloc] init];
[navigationController pushViewController:tableView animated:NO];
The tableview gets added as the rootview to the navigation controller. And then on selecting a row if you wish to push another viewcontroller use the
self.navigationController pushViewController: newViewController animated:YES];
inside the didSelectRowAtIndex method.
NOTE: its UITableViewController *tableView and UINavigationController *navigationController by declaration. So code accordingly for your table.
why UIViewController-> UINavigationBar + UITableView ?
I suggest you another approach
->UITableViewController A -> Embedded with a Navigation Controller, then after populate tableview you can pass data to
->UITableViewController B by
[[self storyboard]instantiateViewControllerWithIdentifier:#"ControllerB"];
then, in storyboard, drop a tableView B and in Identity->storyboard Id put some id.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *sUrl= #"<#string#>";
if (indexPath.row == 0) sUrl= #"http://www.apple.com/";
if (indexPath.row == 1) sUrl= #"http://www.google.com/";
if (indexPath.row == 2) sUrl= #"http://www.times.uk/";
NSMutableArray *selectedObject = [arrayOpenMale objectAtIndex:0];
NSLog(#"%# is the selected object.",selectedObject);
SeriesAdetailVC *dvc = [[self storyboard]instantiateViewControllerWithIdentifier:#"DetailView"];
dvc.strings7 = [NSURL URLWithString:sUrl];
[self.navigationController pushViewController:dvc animated:YES];
}
hope help
I have done a sample app with UISplitViewController studying the example they have provided. I have created three detailviews and have configured them to change by the default means. Either using the left/master view in landscape AND using the popover in the portrait orientation.
Now I am trying to move to another view(previous/next) from the currentView by using left/right swipe in each view. For that, what I did was just created a function in the RootViewController. I copy-pasted the same code as that of the tablerow selection used by the popover from the RootViewController. I am calling this function from my current view's controller and is passing the respective index of the view(to be displayed next) from the current view. Function is being called but nothing is happening.
Plz help me OR is anyother way to do it other than this complex step? I am giving the function that I used to change the view.
- (void) rearrangeViews:(int)viewRow
{
UIViewController <SubstitutableDetailViewController> *detailViewController = nil;
if (viewRow == 0) {
DetailViewController *newDetailViewController = [[DetailViewController alloc] initWithNibName:#"DetailView" bundle:nil];
detailViewController = newDetailViewController;
}
if (viewRow == 1) {
SecondDetailViewController *newDetailViewController = [[SecondDetailViewController alloc] initWithNibName:#"SecondDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
if (viewRow == 2) {
ThirdDetailViewController *newDetailViewController = [[ThirdDetailViewController alloc] initWithNibName:#"ThirdDetailView" bundle:nil];
detailViewController = newDetailViewController;
}
// Update the split view controller's view controllers array.
NSArray *viewControllers = [[NSArray alloc] initWithObjects:self.navigationController, detailViewController, nil];
splitViewController.viewControllers = viewControllers;
[viewControllers release];
if (rootPopoverButtonItem != nil) {
[detailViewController showRootPopoverButtonItem:self.rootPopoverButtonItem];
}
[detailViewController release];
}
I have googled and found a nice approach. They use a UINavigationController on the detail view to push new views. You could use this same solution for your project.
http://www.cimgf.com/2010/05/24/fixing-the-uisplitviewcontroller-template/