I have an app which has a UITabBarController as rootviewcontroller. The UITabBarController has four items. The four UIViewConotrollers are embedded in UINavigationControllers. I can add a button or image on the UINavigationController. However, I have to repeat 4 times on each UINavigationController for the same button or image. Is it possible I can add a button or image on the top of UITabBarController which is set as rootviewcontroller? Thanks in advance.
Theoretically, you can do it by:
UIButton *button = ; // your button
button.frame = CGRect(...); // position on the screen, where you want to have the button
[rootViewController addSubview:button];
However, this solution is strongly not recommended.
What you can do instead, is to create an abstract class MyViewControllerWithButton : UIViewController which will implement viewDidAppear: creation of UIButton and adding it to the navigationBar.
All the viewControllers that you use in tab bars will be a subclass of MyViewControllerWithButton.
Related
I have created a utility app that links by button to another xib called scene - I am trying to create a navigation control for that link. When the button is clicked to then have a 'back' button on my scene xib. I don't wish to have a navigation bar visible on the Main View Controller or the Flipside View Controller. I'm quite new to iOS and I have no idea how to do this?
Would it maybe just be better to have a button going back to menu on a custom HUD? I don't know if that can be done?
Thank you for any help in advance, and thank you for your time
you could create a custom UINavigationBar on your scene xib, and add the custom back button to it if you don't want to create NavigationController , alternate would be that you could just make your first view as NavigationController and push the Scene view over it and it will brings the back button on the child view which is scene, keep your navigationBar hidden when you are on MainViewController and show only on scene view.
For hide UINavigationBar:
[[self navigationController] setNavigationBarHidden:YES animated:YES];
And Your can crate Custom UIButton and put anywhere (As Your requirement).
and in its method, Write code for go back to UIViewController (previous UIVieController).
Such like,
UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
[btnBack addTarget:self action:#selector(goBack:) forControlEvents:UIControlEventTouchUpInside];
btnBack.frame = CGRectMake(10, 10.5, 36, 39); // change it , As your wish
[btnBack setBackgroundImage:[UIImage imageNamed:#"MMBack.png"] forState:UIControlStateNormal];
[self.view addSubview:btnBack];
// call method of UIButton
-(void)goBack:(UIButton *) Sender
{
[self.navigationController popViewControllerAnimated:YES];
}
I make an iphone application that use a segmented Control to switch between 2 viewControllers that display different informations. So, I defined in the first view Controller a segmented Control that I linked in IB to a Segmented Control that I place on the corresponding view.
#interface FirstViewController : UIViewController{
//NSArray * viewControllers;
//UINavigationController * navigationController;
IBOutlet UISegmentedControl *segment; //->segment linked in the nib of FirstViewController
}
The action related to the segmented control is the following:
-(IBAction)valuechanged:(id)sender{
NSInteger index = [(UISegmentedControl *)sender selectedSegmentIndex];
UIViewController *parking=[[ParkingViewController alloc]
initWithNibName:#"ParkingViewController" bundle:nil] ;
viewControllers= [NSArray arrayWithObjects:self,parking,nil];
if(index==1){
UIViewController * incomingViewController = [viewControllers objectAtIndex:index];
[self presentModalViewController:incomingViewController animated:YES];
}
}
In this Action, I define what to do once user click on segmented control.Here, it's loading a new viewController named ParkingViewController. The problem is that once the new ParkingViewController is loaded the segmentedControl disappear and so I can't come back to the firstViewController.
I don't know how to do to keep the segmentedControl for both views?
Thank u all
Quentin
Generally, the UITabBarController is used to control switching between two or more view controllers. It will stay on the screen and allow switching back and forth.
The segmented controller you have cannot stay on the screen when you present a modal view. A modal view will take over the entire screen. Also, because you are animating it onto the screen, a copy of the segmented view controller in the new view will not appear to be the same segmented control because the user saw it scroll onto the screen.
I can't come back to the firstViewController... ??
You can add a UIButton and call an IBAction
[self dismissModalViewControllerAnimated:NO];
Bit confused with this one so bear with me...
I have a Navigation-based project which is working fine. I'm trying to create my first custom UIView to make a couple of buttons which I will use in multiple places. One of the buttons needs to push a viewcontroller into the navigation when it's clicked but I'm not sure how to do this.
When I had the button set up within a view controller I was using:
LocationViewController *controller = [[LocationViewController alloc] initWithNibName:#"LocationViewController" bundle:nil];
[self.navigationController pushViewController:controller animated:YES];
[controller release];
but the self.navigation controller won't work now, will it? How do I access the navigation controller of the viewcontroller that this uiview will be added to?
Hope at least some of that makes sense, as I said it's my first go at subclassing the uiview and adding it to multiple pages so I'm a bit lost.
EDIT TO ADD - I have the button click events inside the custom UIView, so that is where I'm trying to change the viewcontroller from. Should I instead wire up the events in whichever viewcontroller I add the view to?
Usually your appDelegate has a UINavigationController property. You can access it in your custom view like this:
UINavigationController *navController = (MyAppDelegate *)[[[UIApplication sharedApplication]
delegate] navigationController];
But more effective way is to make delegate method for your custom view and handle button action in your viewController.
MyCustomView.h
#protocol MyCustomViewDelegate
#interface MyCustomView : UIView {
id<MyCustomViewDelegate> cvDelegate; }
#property(nonatomic, assign) id<MyCustomViewDelegate> cvDelegate;
#protocol MyCustomViewDelegate #optional
-(void)didClickInCustomView:(MyCustomViewDelegate*)view withData:(NSObject*)data;
#end
MyCustomView.m
- (void)myButtonClick:(id)sender
{
[self.cvDelegate didClickInCustomView:self withData:someData];
}
So now you can handle this event in any place where is your custom
view.
Add the button from the interface builder or from the view controller's viewDidLoad using code:
CGRect frame = CGRectMake(0, 0, 24, 24);
UIButton *button = [[UIButton alloc] initWithFrame:frame];
[button addTarget:self action:#selector(handleMyButton:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
Then implement -(void)handleMyButton:(id)sender {}; in your view controller. Or you could instead write -(IBAction)handleMyButton:(id)sender {}; and link method and button using the interface builder.
Then inside the method just paste the block of code you posted above. If you started with the Xcode navigation controller template project it should work.
I think it's cleaner to hide the designated initializer initWithNibName: because it is an implementation detail.
When you say you are subclassing the UIView I don't know exactly what you mean. If you want to add another view controller with a custom view just use the UIViewController template and customize the XIB file, no need to subclass an UIView unless you are really modifying its behaviour, which I guess you are not. The view is a view, and the controller stuff like handling buttons should be in the controller.
The actual controller need to be in the navigation controller stack to be able to push another controller.
Or you can make a new navigation controller instance and push your LocationViewController.
I am using a UITabBar control from library in one of my view (note that I am not using UITabBarController but the UITabBar control).
Now, I am adding two tabBar items to this tabBar.
I have created controller class for this view (.m and .h) files and used delegates in the .h file.
In the .m file I have used the following function:
(void)tabBar:(UITabBar *)TabBarControl didSelectItem:(UITabBarItem *)FirstView
I have assigned tag = 0 and tag = 1 to respective tabBar items.
What I want to do is that, on click of first tabBar item I want to show a view and click of another tabBar item, I want to show another view.
So, in the above function I am checking that if the tag of clicked tabBar item is 0 than I will show one view else I will show another view.
I am showing the view as following:
Team1Scoreboard *tempTeam1Scoreboard = [Team1Scoreboard alloc];
tempTeam1Scoreboard = [tempTeam1Scoreboard initWithNibName:#"UserTeamScoreboard" bundle:[NSBundle mainBundle]];
self.cntrlTeam1Scoreboard = tempTeam1Scoreboard;
[tempTeam1Scoreboard release];
UIView *theWindow = [self.view superview];
[self.view removeFromSuperview];
[theWindow addSubview:self.cntrlTeam1Scoreboard.view];
Now the problem is that, when I click on any of the tabBar item, it will load the correct view but the tabBar itself will be disappeared as I am adding the view to window itself.
Please help me so that I can load correct view and also my tabBar itself is visible.
The TabBar is disappearing because it's a child of the view which you are then adding a new child to and the new child is sized the same as the parent. Did that make sense? Ok, look at it this way:
You have ViewA and ViewA has a couple of labels and a TabBar. ViewA is managed by ViewControllerA. In ViewControllerA you are creating an instance of ViewB and calling ViewControllerA.view addSubView:instanceOfViewB, right? Before doing that, you will want to resize ViewB.
Try something like this:
ViewControllerB *viewControllerB = [[ViewControllerB alloc]initWithNibName:#"ViewB" bundle:nil];
CGRect frame = CGRectMake(self.view.frame.origin.x,
self.view.frame.origin.y,
self.view.frame.size.width,
self.view.frame.size.height - 40);
viewControllerB.view.frame = frame;
[self.view addSubview:viewB.viewControllerB];
Basically it should be close to what you are doing, but I'm setting the size to be 40 px less (whatever you need to remove the tab bar).
I have a UIViewController class with a navigation bar shown and a button on the rightmost end of the navigation bar. Then right underneath it is my main content area with a UISegmentedControl shown below the navigation bar.
Whenever I click/tap on the navigation bar near the right button but not directly on the button, I am getting an unexpected button press event. Usually, it's when I'm trying tap on the right most segment item that happens to be placed beneath the button. So the tap is on the button instead of the segment. How do I make sure the event is directed to the right receiver?
My UI is created with Interface Builder and the UISegmentedControl is hooked up to my class in the nib file.
My class:
#interface MyViewController : UIViewController
{
IBOutlet UISegmentedControl *segmentedControl;
}
#end
#implementation MyViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
UIBarButtonItem *button = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:self action:#selector(onButtonPressed:)];
self.navigationItem.rightBarButtonItem = button;
[button release];
}
- (IBAction)onButtonPressed:(id)sender
{
NSLog(#"onButtonPressed reached!");
}
- (IBAction)onSegmentItemPressed:(id)sender
{
NSLog(#"onSegmentItemPressed for: %d", [segmentedControl selectedSegmentIndex]);
}
#end
This is standard behavior. It is designed to make those small buttons easier to press. You can try it in any app with a navbar and you will get the same result. Without seeing the layout of your app, I would think you are either missing the segment (try tapping more carefully) or the segment is too close to the navbar button (move it slightly).
It may be possible to use the hitTest: methods to determine whether or not the touch was in the segment, and manually activate it, but I would think this is more trouble than it's worth.
You may need to move the segmented control's origin further down, vertically, away from the navigation bar button item, to prevent unexpected taps.