I have set up two barbutton items one on the right and one on the left. The right one successfully changes between two views, and the second one ont the right is a segmented control which is supposed to change between two views like wise. Here is my code, i want to know how to implement changing views with the segmented control.
- (void)setupNavigationBar {
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44.0f)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:#"ContainerView"];
navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFastForward target:self action:#selector(switchViewControllers)];
navBar.items = [NSArray arrayWithObject:navItem];
segmented = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"seg1.png"],
[UIImage imageNamed:#"seg1.png"],
nil]];
segmented.frame = CGRectMake(0, 0, 50, 30);
segmented.segmentedControlStyle = UISegmentedControlStyleBar;
UIBarButtonItem * segmentedBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmented];
navItem.leftBarButtonItem = segmentedBarItem;
[self.view addSubview:navBar];
}
- (void)viewDidLoad {
[super viewDidLoad];
// This is just for the purpose of calling the switch method.
[self setupNavigationBar];
CGRect rect = CGRectMake(0, 44, 320, 436);
currentViewController = [[FirstViewController alloc] init];
currentViewController.view.frame = rect;
nextViewController = [[SecondViewController alloc] init];
nextViewController.view.frame = rect;
[self addChildViewController:currentViewController];
[self addChildViewController:nextViewController];
[self.view addSubview:currentViewController.view];
}
- (void)switchViewControllers {
[self transitionFromViewController:currentViewController toViewController:nextViewController duration:3.0 options:UIViewAnimationTransitionFlipFromRight | UIViewAnimationOptionCurveEaseInOut animations:nil completion:^(BOOL finished) {
UIViewController *tempViewController = currentViewController;
currentViewController = nextViewController;
nextViewController = tempViewController;
tempViewController = nil;
}];
}
How would I implement the segmented control to change views?
For add UISegmentedControl on UINavigationBar
EDIT :
NSArray *itemArray = [NSArray arrayWithObjects:
[UIImage imageNamed:#"1.png"],
[UIImage imageNamed:#"2.png"],
nil];
UISegmentedControl* menuSegmentedButton = [[UISegmentedControl alloc] initWithItems:itemArray];
menuSegmentedButton.momentary = YES;
menuSegmentedButton.selected = NO;
menuSegmentedButton.segmentedControlStyle = UISegmentedControlStyleBar;
[menuSegmentedButton addTarget:self action:#selector(doMenu)
forControlEvents:UIControlEventValueChanged];
[menuSegmentedButton sizeToFit];
UIBarButtonItem* barButton = [[UIBarButtonItem alloc]
initWithCustomView:menuSegmentedButton];
self.navigationItem.leftBarButtonItem = barButton;
Add this method for segment button tapped
-(void)doMenu
{
/// code for segment button tapped
}
I guess performance issues may require having separate view controllers rather than two subviews but it is much more complex - You would have a very complicated navigation system and would need to set the segment control for each view. I am sure there is another way to subclass a view but I am unfamiliar with this so would suggest having one view controller containing your views.
Note: If you were to programmatically create all the objects in each view, you could achieve great performance by removing objects from any hidden views.
So first you have a UISegmentedControl (as created in iPatel's answer). But add in this line [ctrl addTarget:self action:#selector(switchViewControllers) forControlEvents:UIControlEventValueChanged];
Then in switchViewControllers put:
if (ctrl.selectedSegmentIndex == 0) {
NSLog(#"One");
self.viewOne.hidden = NO;
self.viewTwo.hidden = YES;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:#"showView" forKey:#"viewOne"];
[defaults synchronize];
}
else if (ctrl.selectedSegmentIndex == 1) {
NSLog(#"Two");
}
-(void)viewDidLoad {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([[defaults objectForKey:#"showView"] isEqualToString:#"viewOne"]) {
self.viewOne.hidden = NO;
self.viewTwo.hidden = YES;
}
else if (...) {
}
}
The above should work when you toggle the segment control. In each if you can toggle the visibility of two views. You would create the different views as a subview of the main view; one on top of the other. You would need to hide one view either in Interface Builder or in the viewDidLoad (I would suggest storing the current view in NSUserDefaults and showing that view when the user first loads the view controller).
Related
I've made a project and implemented the folder project and now I've set up a subview which is displayed aboce the folder like on the picture. On the popup there's a image and I would like to do it like this that if I tap the image a new controller with the whole image will displayed. I've set up all correctly...However, I tried to change the subview etc. THIS Navigationcontroller never appears at the top of all "layers". it appears in the folderviewcontroller. By the way, the Navcontroller is set up by this project: https://github.com/mwaterfall/MWPhotoBrowser and the Popup was set up through this: https://github.com/kgn/KGModal
So here's my code:
-(IBAction)mehr:(id)sender {
UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 440)];
cubut = [UIButton buttonWithType:UIButtonTypeCustom];
[cubut addTarget:self
action:#selector(dothis:)
forControlEvents:UIControlEventTouchUpInside];
[cubut setTitle:#"Show View" forState:UIControlStateNormal];
cubut.frame = CGRectMake(5, 70, 270, 200);
[contentView addSubview:cubut];
- (IBAction)dothis:(id)sender {
MWPhotoBrowser *browser = [[MWPhotoBrowser alloc] initWithDelegate:self];
// Set browser options.
browser.wantsFullScreenLayout = YES;
browser.displayActionButton = YES;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:browser];
[self presentModalViewController:navController animated:YES];
NSMutableArray *photos = [[NSMutableArray alloc] init];
MWPhoto *photo;
photo = [MWPhoto photoWithFilePath:[[NSBundle mainBundle] pathForResource:#"star" ofType:#"png"]];
photo.caption = #"The star is soo beateful...";
[photos addObject:photo];
self.photos = photos;
}
- (MWPhoto *)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index {
if (index < _photos.count)
return [_photos objectAtIndex:index];
return nil;
}
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser {
return _photos.count;
}
And the picture:
(The button is set to custom so you cant see it...it has the same frame like the image)
So this was not finished so I have to awnser my own questions that I didnt found a solution for this.
This looks like a bug to me, but maybe someone can think of a workaround?
Basically if you have a custom UIToolbar, its button items will automatically hide when you present a UIActivityViewController, and reappear when you dismiss it. This is only the case on the iPhone. Being that UIActivityViewController doesn't hide the entire screen it just looks weird that buttons disappear behind the dimmed screen.
To replicate, simply start a single view project and use the following code on the view controller:
- (void)viewDidLoad {
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 40)];
UIBarButtonItem *button = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:#selector(didTapAction)];
toolbar.items = [NSArray arrayWithObject:button];
[self.view addSubview:toolbar];
}
- (void)didTapAction {
NSArray *items = [NSArray arrayWithObjects:#"Text", nil];
UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
[self presentViewController:sharing animated:YES completion:nil];
}
Found a workaround. Simply get rid of all the items before presenting, and add them back right after.
- (void)didTapAction {
NSArray *items = [NSArray arrayWithObjects:#"Text", nil];
UIActivityViewController *sharing = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:nil];
NSArray *barItems = toolbar.items;
toolbar.items = nil;
[self.navigationController presentViewController:sharing animated:YES completion:nil];
toolbar.items = barItems;
}
know it is quite old thread for but those who look this page for solution, here you go :
With iOS7, you can use this approach to show/hide your toolbar button :
if(// your code Condition)
{ self.toolbarBtn1.enabled = YES;
self.toolbarBtn1.tintColor = nil; }
else
{ self.toolbarBtn1.enabled = NO;
self.toolbarBtn1.tintColor = [UIColor clearColor]; }
I need to display a view modally. The viewcontroller that needs to be displayed modally has to have a UIToolbar at the bottom. In this toolbar there are one uisegmentedcontroller with three elements. (Think of an tabbar).
In the viewcontroller that presents the modal viewcontroller I have:
-(IBAction)presentModally:(id)sender {
if (self.nvc == nil) {
MyModalViewController *vc = [[MyModalViewController alloc] init];
UINavigationController *navvc = [[UINavigationController alloc] initWithRootViewController:vc];
navvc.navigationItem.prompt = #"";
navvc.navigationBar.barStyle = UIBarStyleBlack;
[vc release];
self.nvc = navvc;
[navvc release];
}
[self presentModalViewController:self.nvc animated:YES];
}
MyModalViewController:
- (void)loadView {
[super loadView];
UIView *uiview = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 320.0f, 460.0f)];
uiview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
self.view = uiview;
[uiview release];
UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0.0f, 436.0f, 320.0f, 44.0f)];
toolbar.barStyle = UIBarStyleBlack;
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth;
NSArray *itemArray = [NSArray arrayWithObjects: #"One", #"Two", #"Three", nil];
UISegmentedControl *segCon = [[UISegmentedControl alloc] initWithItems:itemArray];
segCon.frame = CGRectMake(60, 4, 200, 36);
segCon.segmentedControlStyle = UISegmentedControlStyleBar;
segCon.tintColor = [UIColor darkGrayColor];
segCon.selectedSegmentIndex = 0;
[segCon addTarget:self action:#selector(changedSegment:) forControlEvents:UIControlEventValueChanged];
[toolbar addSubview:segCon];
self.segmentedControl = segCon;
[segCon release];
[[self navigationController].view addSubview:toolbar];
[toolbar release];
}
- (void)changedSegment:(id)sender {
UISegmentedControl *control = (UISegmentedControl *)sender;
int index = control.selectedSegmentIndex;
[[self navigationController] popViewControllerAnimated:NO];
[[self navigationController] pushViewController:[self.controllers objectAtIndex:index] animated:NO];
}
The viewcontrollers in the array are just normal UIViewControllers.
I have set this property in those classes to:
self.navigationItem.hidesBackButton = YES;
My question: Is this the proper way to achieve a UITabBarController behavior?
Haven't tested it, but It looks good, except that popViewControllerAnimated. I'd use popToRootViewControllerAnimated instead (In case a controller uses itself the navigationController).
I use a UITableViewController to display a list of items, and one of the items selection must lead to a
photo gallery created with animationImages. I usually declare the controllers associated to
the first level selection with something like :
MyController *myController = [[MyController alloc] initWithStyle:UITableViewStylePlain];
myController.title = #"title 1";
myController.rowImage = [UIImage imageNamed:#"myControllerIcon.png"];
..
But I believe a UIViewController is needed to use animationImages..
How can I display a image animation directly from a table view ?
If you have you image sequence build in this manner:
NSArray *myImages = [NSArray arrayWithObjects:
[UIImage imageNamed:#"myImage1.png"],
[UIImage imageNamed:#"myImage2.png"],
[UIImage imageNamed:#"myImage3.png"],
[UIImage imageNamed:#"myImage4.gif"],
nil];
UIImageView *myAnimatedView = [UIImageView alloc];
[myAnimatedView initWithFrame:[self bounds]];
myAnimatedView.animationImages = myImages;
myAnimatedView.animationDuration = 0.25; // seconds
myAnimatedView.animationRepeatCount = 0; // 0 = loops forever
[myAnimatedView startAnimating];
[self addSubview:myAnimatedView];
[myAnimatedView release];
Snatched from here
Then you can build your own cell or add the animationImage to the cell by adding it to either:
[cell setImage:myAnimatedView];
[cell setBackgroundView:myAnimatedView];
[cell setSelectedBackgroundView:myAnimatedView]
I Created Segment Control through Interface Builder.
Created a IBAction and Linked to Value Changed Option of segment Controller.
- (IBAction)GenderBttonAction:(id)sender {
printf("\n Segemt Controll");
}
When i click on segment controller this method is calling , but how would i get the seleced index value of segment controller.
Please help me dears.
((UISegmentedControl *)sender).selectedSegmentIndex;
:-)
I use the following code in a view controller.m to say launch a modal controller and it seems to work good for me.
- (void)viewDidLoad
{
NSArray *segmentContent = [NSArray arrayWithObjects:
NSLocalizedString(#"view 1", #""),
NSLocalizedString(#"view 2", #""),
NSLocalizedString(#"Close", #""),
nil];
//or images insted of text
//NSArray *segmentContent = [NSArray arrayWithObjects:
// [UIImage imageNamed:#"pic.png"],
// [UIImage imageNamed:#"spic.png"],
// [UIImage imageNamed:#"close.png"],
// nil]];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:segmentContent];
segmentedControl.selectedSegmentIndex = UISegmentedControlNoSegment;
segmentedControl.momentary = YES; // option
segmentedControl.autoresizingMask = UIViewAutoresizingFlexibleWidth;
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.frame = CGRectMake(0, 0, 160, 30);
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
UIBarButtonItem *segments = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segments;
self.navigationItem.title = #"My title";
[segments release];
Then add the actions on selection, last one is a close statement if you launched a modal controller.
- (IBAction)segmentAction:(id)sender
{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
switch ([sender selectedSegmentIndex])
{
case 0:
{
// Do stuff like launch a modal controller. But don't forget to add this all into your modal controller views to get back out :)
InfoViewController *infoViewController = [[InfoViewController alloc] initWithNibName:#"InfoViewController" bundle:nil];
UINavigationController *aInfoViewController = [[UINavigationController alloc] initWithRootViewController:infoViewController];
[self presentModalViewController:aInfoViewController animated:YES];
break;
}
case 1:
{
// do stuff
break;
}
case 2:
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
break;
}
}
NSLog(#"Segment clicked: %d", segmentedControl.selectedSegmentIndex);
}
Use the method below if needed to close the modal via this way.
- (IBAction)dismissAction:(id)sender
{
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
and don't forget to declare the action/method in the same respective h file.
- (IBAction)dismissAction:(id)sender;
Hope this helps.
Sincerely, Kirk