I have an overlay in which I've created a toolbar containing a done button.
The name of the selector for done button is doneButtonPressed.
Now when I click done button, how do I get a new nib view? Let's us assume I created a nib named TestViewController.
-(void)doneButtonPressed {
//What goes here?
}
If you want to instantiate a UIViewController from a nib you can use the following line of code:
UIViewController *viewControllerName = [[UIViewController alloc] initWithNibName:(NSString *) bundle:(NSBundle *)];
Then you can present the view modally with:
[self presentModalViewController:(UIViewController *) animated:(BOOL)];
Now if you are using a storyboard, then you could just instantiate a view controller from a storyboard object.
Not sure if this is what you are looking for, but you could turn your view into a "container view". To do that you will need to create subviews and then just switch them out. I did this when I wanted a menu to control a different view.
[self.view insertSubview:(view you want to insert) atIndex:0];
Then when you want to switch it out:
[[[self.view subviews] objectAtIndex:0] removeFromSuperview];
[self.view insertSubview:(view you want to replace with) atIndex:0];
EDITED
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(doneButtonPressed:)];
- (void)doneButtonPressed {
SkipQViewController *skipQ = [[SkipQViewController alloc] initWithNibName:#"Name" bundle:bundleName];
[self presentModalViewController:skipQ animated:YES];
}
If that does not work try using: SkipQViewController *skipQ = [[SkipQViewController alloc] init]; to instantiate your view controller.
-EDIT-
Instantiate the view controller as such: SkipQViewController *skipQ = (SkipQViewController *)[[UIViewController alloc] initWithNibName:#"sqvc" bundle:nil];
Related
Please excuse my newness in IOS, but I am trying to switch view controllers when someone clicks on a button in my toolbar. For my toolbar I have overridden the UIToolbar and drawn my own custom toolbar. I have four buttons each with their own action something like this:
NSMutableArray *toolbarItems = [#[] mutableCopy];
[toolbarItems addObject:[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"notifications"] style:UIBarButtonItemStyleBordered target:self action:#selector(viewNotifications)]];
I want to be able to do something like:
-(void)viewNotifications
{
NSLog(#"CustomUIToolbar::viewNotifications");
//layoutFlow....
// Show the notifications view controller
NotificationsViewController *rootViewController = [[NotificationsViewController alloc] initWithCollectionViewLayout:layoutFlow];
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
[self presentViewController:navController animated:YES completion:NULL];
}
The problem with this is clearly that the UIToolbar doesn't directly have access to switching view controllers. Is there a way to access the presentViewController method or something like it from within the custom UIToolbar?
Create a protocol and a delegate and let the UIViewController which creates the UIToolBar implement that delegate.
When the user presses a UIBarButtonItem you send the delegate the message (the main UIViewController) and handle the UINavigationController code there.
#protocol ToolBarProtocol <NSObject>
-(void)didPressButton1;
#end
#property (nonatomic) id <ToolBarProtocol> delegate
And when creating your UIToolBar:
YourToolBar *toolbar = [YourToolBar alloc] init];
toolbar.delegate = self;
Inside your method change to tell the delegate what should happen:
-(void)viewNotifications
{
NSLog(#"CustomUIToolbar::viewNotifications");
//layoutFlow....
if ([self.delegate respondsToSelector:#selector(didPressButton1)])
{
[self.delegate didPressButton1];
}
}
The Application I am working on has dialogs/UI entirely done by hand/code.
Presently it only has Views/ViewControllers and no NavigationControllers yet.
"Navigation" is done by displaying controllers on top of another, and/or hiding views.
I am trying to display a ABUnknownPersonViewController.
I manage to do it, but I cannot dismiss it; I tried to add a button but it did not even showed up. I am using the following code, but I do not see any dismiss/cancel in my UIViewController, how should I modify it ?
- (void)AddCallerToAdressBook
{
ABUnknownPersonViewController *controller = [[ABUnknownPersonViewController alloc] init];
controller.allowsAddingToAddressBook = YES;
UINavigationController *newNavigationController = [[UINavigationController alloc] initWithRootViewController:controller ];
newNavigationController.title=#"AddCaller";
UIBarButtonItem *leftBarButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self action:#selector(dismissed)];
[newNavigationController.navigationItem setLeftBarButtonItem:leftBarButton];
[self presentModalViewController:newNavigationController animated:YES];
}
I need to have a tabbar kind of view in a view controller(say view controller 3) which is in navigation stack and will be pushed by another view controller (say view controller 2).
I added tool bar to view controller 3 with many buttons. But managing and switching between views seems difficult.
How can i add a tabbar controller in the middle of navigation stack.
please help me out.
You cnt create tabbar in the middle of view controllers, you can create tab bar structure using custom segment controller. check the link below for creating custom segment controller.
https://www.cocoacontrols.com/search?q=segment
You cannot add TabBar there - tabBar is only designed to be at the bottom.
You have to create your own ViewController and add it as subView for navigationViewController and implement whole switching methods.
There has been some discussion about this design in related questions.
The way I understand your question it's definitely possible though. I am pushing a UITabBarController onto a UIViewController that is embedded in a UINavigationController. I did everything in the interfacebuilder except for the push which I implemented programmatically.
So in ViewController.m I added an action to a button:
- (IBAction)searchForPlacesButtonPressed:(id)sender {
TabBarController *tc = [[self storyboard] instantiateViewControllerWithIdentifier:#"TabBarController"];
tc.title = #"Orte";
tc.tabBar.selectedImageTintColor = [UIColor colorWithRed:(28.0f/255.0f) green:(100.0f/255.0f) blue:(52.0f/255.0f) alpha:1.0f];
UIBarButtonItem * logo = [[UIBarButtonItem alloc] initWithCustomView:[[UIImageView alloc] initWithImage:[UIImage imageNamed:#"icon_xyz"]]];
tc.navigationItem.rightBarButtonItem = logo;
Places *d = [[Places alloc] init];
for (UIViewController *vc in tc.viewControllers) {
if ([vc isKindOfClass:[MapViewController class]]) {
((MapViewController *)vc).dataHandler = d;
((MapViewController *)vc).mapViewDelegate = d;
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Karte" image:[UIImage imageNamed:#"map"] tag:0];
}
else if ([vc isKindOfClass:[TableViewController class]]) {
((TableViewController *)vc).dataHandler = d;
vc.tabBarItem = [[UITabBarItem alloc] initWithTitle:#"Liste" image:[UIImage imageNamed:#"eye"] tag:1];
}
}
[self.navigationController pushViewController:tc animated:YES];
}
Using the tabs at the bottom you can switch between viewing the places on a map and listed in a table.
I have made a barbuttonitem programmatically on a toolbar overlay. All of this appears when my camera is accessed. The button functions fine and the selector is fully capable of calling it.
Here's the overlay in .m , take a look at the doneButton selector.
- (UIView*)CommomOverlay {
//Both of the 428 values stretch the overlay bottom to top for overlay function. It
doesn't cover it.
UIView* view = [[UIView alloc] initWithFrame:CGRectMake(0,0,320,420)];
UIImageView *FrameImg = [[UIImageView alloc] initWithFrame:CGRectMake(0,0,320,420)];
[FrameImg setImage:[UIImage imageNamed:#"newGraphicOverlay.png"]];
FrameImg.userInteractionEnabled = YES;
UIToolbar *myToolBar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 428, 320, 50)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self
action:#selector(doneButtonPressed)];
[myToolBar setItems:[NSArray arrayWithObjects: cancelButton, flexiSpace2, flexiSpace,
doneButton, nil] animated:YES];
[view addSubview:FrameImg];
[view addSubview:myToolBar];
return view;
}
Here's method I'm using for my void function.
//NewViewController is the nib I want to bring up when done is clicked.
-(void)doneButtonPressed {
NewViewController *UIView = [[NewViewController alloc] initWithNibName:nil bundle:nil];
UIView.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentModalViewController:UIView animated:NO];
[UIView release];
NSLog(#"Acknowledged!");
}
The button is being called, but nothing happens. As you can see I added an NSLog function to see whether it was being called or not. "Acknowledged" shows up in the log when the button is clicked, so it's not a problem with the selector. The app doesn't crash when i use the button either, which should occur had I done some heavily faulty coding (in fact I don't even get any warnings about it), as far as Xcode is considered, everything is fine which, needless to say isn't.
I also have my .h files of NewViewController in place correctly in the view controller in which this code is being implemented.
UIView is the name of a class. Don't use it as the name of a variable. At best it is hugely confusing and is a probable reason for your problems. Rename UIView to something like myView everywhere in doneButtonPressed.
The structure is as follows:
View
Tab Bar Controller
Navigation Controller
View Controller
Navigation Controller
View Controller
Navigation Controller
View Controller
Navigation Controller
View Controller
Navigation Controller
View Controller
Navigation Controller
View Controller
The above controllers have been initialised in interface builder.
What I'm trying to do is add a right UIBarButtonItem to each navigation controller but not having any success.
Here's what I've tried:
- (void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor clearColor];
self.view.opaque = NO;
self.tabBarController.view.frame = self.view.bounds;
NSArray *currentViewControllers = self.tabBarController.viewControllers;
NSMutableArray *updatedViewControllers = [NSMutableArray array];
for (int i=0; i<currentViewControllers.count; i++) {
UINavigationController *tempNav = [[UINavigationController alloc]init];
tempNav = [currentViewControllers objectAtIndex:i];
UIBarButtonItem *dismissButton = [[UIBarButtonItem alloc]
initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(dismissLibraryBrowser)];
tempNav.navigationItem.rightBarButtonItem = dismissButton;
[updatedViewControllers addObject:tempNav];
[dismissButton release];
[tempNav release];
NSLog(#"Added controller number %d",i);
}
self.tabBarController.viewControllers = [NSArray arrayWithArray:updatedViewControllers];
[self.view addSubview:tabBarController.view];
}
The code executes without any errors, but the button doesn't appear. I'm sure I've misunderstood something here. Would appreciate some guidance.
You are over complicating things slightly with recreating viewControllers and temporary arrays. You just need to manipulate the objects loaded from the nib
[self.tabBarController.viewControllers enumerateObjectsUsingBlock:^(UINavigationController *navigationController, NSUInteger idx, BOOL *stop) {
UIViewController *rootViewController = [navigationController.viewControllers objectAtIndex:0];
UIBarButtonItem *rightBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:#"Done"
style:UIBarButtonItemStyleBordered
target:self
action:#selector(dismissLibraryBrowser)];
rootViewController.navigationItem.rightBarButtonItem = rightBarButtonItem;
}];
As for the structure of your app - the docs for UITabBarController say
When deploying a tab bar interface, you must install this view as the root of your window. Unlike other view controllers, a tab bar interface should never be installed as a child of another view controller.
So I would suggest having a look at restructuring some stuff, if you need it only occasionally why not consider presenting it modally?