UIToolbar and UINavigationController - iphone

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).

Related

PageCurlEffect in pageviewcontroller overlaps toolbar

Using Pagecurleffect in pageviewcontroller whenever i turn page it overlaps toolbar.
This is my code
- (void)viewDidLoad {
[super viewDidLoad];
modelArray = [[NSMutableArray alloc] init];
for (int index = 1; index <= totalPages; index++) {
[modelArray addObject:[NSString stringWithFormat:#"%i", index]];
}
thePageViewController = [[UIPageViewController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation: UIPageViewControllerNavigationOrientationHorizontal options:nil];
thePageViewController.delegate = self;
thePageViewController.dataSource = self;
thePageViewController.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
contentViewController = [[ContentViewController alloc] initWithPDF:PDFDocument];
contentViewController.page = [modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:contentViewController];
[thePageViewController setViewControllers:viewControllers direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
[self addChildViewController:thePageViewController];
[self.view addSubview:thePageViewController.view];
thePageViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
[thePageViewController didMoveToParentViewController:self];
}
In the content view controller using this code in viewdidload
- (void)viewDidLoad
{
[super viewDidLoad];
// Create our PDFScrollView and add it to the view controller.
CGPDFPageRef PDFPage = CGPDFDocumentGetPage(thePDF, [_page intValue]);
pdfScrollView = [[PDFScrollView alloc] initWithFrame:CGRectMake(0, 46, self.view.bounds.size.width, 915)];
[pdfScrollView setPDFPage:PDFPage];
[self.view addSubview:pdfScrollView];
self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
pdfScrollView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
_toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 965, self.view.bounds.size.width, 40)];
_toolbar.barStyle = UIBarStyleBlackOpaque;
[self.view addSubview:_toolbar];
}
Not looking good when it overlaps toolbar while turning page. How i can fix this.
I want like second image
It is turning page under the navigation bar not overlapping navigation bar.
Thanks
You are adding a toolbar in each page, and it's inside the page.
You have to create the toolbar on your first block of code, and add [self.view bringSubviewToTop:_toolbar];at the end

how to load picker view values from sqlite database in iPhone

I'm just a beginner in iPhone programming.
I have added 3 uipickers in one view.
First picker for employee.
Second picker is for product.
Third picker for customer.
I have created 3 tables in sqlite for the above.
And I have to load the values to pickers from the tables.
Have I to keep 3 pickers in XiB file?
I want the pickers to be displayed when i click on the table cell.(I have a table view obviously...where I will choose the desired value from the picker)
For time being I have added the array statically. But I wanted to load it from the sqlite database.
How to connect?
How to display the values?
What should I do?
Can you please help me.
I've been meddling with it for 4 days. Can Someone please help....
Cathi
Create 3 UiPickerView using Code after the Database read is done
Set The values according to the database
Set separate Tags (1,2,3) for each UIPicker. Then you can get it again every where.
Set the UiPickerView delegates.
Use [yourPicker selectRow:10 inComponent:0 animated:YES]; to set picker selected value
Sample Code to Add UIPicker
-(void)displayPicker{
#try {
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(pickerCancel:)];
cancelBtn.tag = 1;
[barItems addObject:cancelBtn];
[cancelBtn release];
cancelBtn = nil;
/* Uncomment this line to add label to Timer/Counter.
NSString *popUpTitleText = [self getPopUpTitleText:#"My Text"];
UIBarButtonItem *titleBtn = [[UIBarButtonItem alloc] initWithTitle:popUpTitleText style:UIBarButtonItemStylePlain target:nil action:nil];
[barItems addObject:titleBtn];
[titleBtn release];
titleBtn = nil;
*/
UIBarButtonItem *flexItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
flexItem.width = 64;
[barItems addObject:flexItem];
[flexItem release];
flexItem = nil;
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(pickerDone:)];
doneBtn.tag = 1;
[barItems addObject:doneBtn];
[doneBtn release];
doneBtn = nil;
[pickerToolbar setItems:barItems animated:YES];
[barItems release];
barItems = nil;
CGRect pickerFrame = CGRectMake(0, 40, 0, 216);
UIPickerView *pickerView = [[UIPickerView alloc] initWithFrame:pickerFrame];
pickerView.showsSelectionIndicator = YES;
pickerView.tag = 1;
pickerView.dataSource = self;
pickerView.delegate = self;
CGRect pickerRect = pickerView.bounds;
pickerView.bounds = pickerRect;
UIViewController* popoverContent = [[UIViewController alloc] init];
UIView* popoverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 344)];
popoverView.backgroundColor = [UIColor whiteColor];
pickerView.frame = CGRectMake(0, 44, 200, 216);
[pickerView selectRow:1 inComponent:0 animated:YES]; //set your selected (Database) value here.
[popoverView addSubview:pickerToolbar];
[popoverView addSubview:pickerView];
popoverContent.view = popoverView;
//resize the popover view shown
//in the current view to the view's size
popoverContent.contentSizeForViewInPopover = CGSizeMake(200, 244);
//create a popover controller
popoverController.delegate = nil;
if (popoverController.popoverVisible == YES) {
[popoverController dismissPopoverAnimated:YES];
}
[popoverController release];
popoverController = nil;
popoverController = [[UIPopoverController alloc] initWithContentViewController:popoverContent];
CGRect popoverRect = [self.view convertRect:yourFrame(CGrect)
fromView:[yourView superview]];
popoverRect.origin.y = popoverRect.origin.y + 15;
popoverController.delegate = self;
popoverRect.size.width = MIN(popoverRect.size.width, 100) ;
popoverRect.origin.x = float yourXvalue;
[popoverController presentPopoverFromRect:popoverRect inView:self.view permittedArrowDirections:UIPopoverArrowDirectionUp animated:YES];
//release the popover content
[popoverView release];
[popoverContent release];
[pickerToolbar release];
[pickerView release];
popoverView = nil;
popoverContent = nil;
pickerToolbar = nil;
pickerView = nil;
}
#catch (NSException * ex) {
NSLog(#"Exception in YourClass Method: displayPicker() Name:%# Reason:%#",[ex name],[ex reason]);
}
}

when UISegmentcontrol item changing, why it will hide customBadge in IOS 5?

I have added custom Badge on UISegmentControl. In Xcode 4.0.2 SDK 4.3 its working fine. But in Xcode 4.2 SDK 5.0 custom badge is not shown when another item is selected. I dont know why this happens? I'm using following code for IOS 4.3 Xcode 4.0.2:
- (void)viewDidLoad {
[super viewDidLoad];
super.tableView.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"wallpaper.png"]];
NSLog(#"Root View Loaded");
segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
#"Home",#"Surveys",#"Results",#"Create",#"Settings",
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 310, 40);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.tintColor = [UIColor darkGrayColor];
segmentedControl.momentary = NO;
segmentedControl.highlighted = YES;
segmentedControl.selectedSegmentIndex = 0;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
NSArray *segmentBarArray = [NSArray arrayWithObjects:
segmentBarItem,nil];
[[self appDelegate] setSegmentedControl:segmentedControl];
[[self appDelegate] setSegmentBarArray:segmentBarArray];
[self setToolbarItems:[[self appDelegate] segmentBarArray] animated:NO];
[[self navigationController] setToolbarHidden:NO animated:YES];
[self.navigationItem setHidesBackButton:YES animated:YES];
self.navigationController.toolbar.tintColor = [UIColor blackColor];
int surveycount = [[self appDelegate] getUnreadSurveyCount];
surveyCountBadge = [CustomBadge customBadgeWithString:[NSString stringWithFormat:#"%d",surveycount]];
[surveyCountBadge setFrame:CGRectMake(105, -10, surveyCountBadge.frame.size.width, surveyCountBadge.frame.size.height)];
[segmentedControl addSubview:surveyCountBadge];
if (surveycount == 0) {
[surveyCountBadge setHidden:YES];
}else{
[surveyCountBadge setHidden:NO];
}
}
-(void)segmentAction:(id) sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *)sender;
NSLog(#"selected index is %d",segmentedControl.selectedSegmentIndex);
if (segmentedControl.selectedSegmentIndex == 0) {
[self.navigationController popToRootViewControllerAnimated:YES];
} else if(segmentedControl.selectedSegmentIndex ==1){
surveyForMeViewController = [[SurveyForMeView alloc] initWithNibName:#"SurveyForMeView" bundle:nil];
[self.navigationController pushViewController:surveyForMeViewController animated:YES];
} else if(segmentedControl.selectedSegmentIndex ==2){
resultViewController = [[ResultView alloc] initWithNibName:#"ResultView" bundle:nil];
[self.navigationController pushViewController:resultViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==3) {
newSurveyViewController = [[NewSurveyView alloc] initWithNibName:#"NewSurveyView" bundle:nil];
[self.navigationController pushViewController:newSurveyViewController animated:YES];
} else if (segmentedControl.selectedSegmentIndex ==4) {
settingsViewControlle = [[SettingsView alloc] initWithNibName:#"SettingsView" bundle:nil];
[self.navigationController pushViewController:settingsViewControlle animated:YES];
}
}
Any thing else should I mention in this?
You are adding a subview to a segmentedControl. As you don't know how the system redraws the control when another item is selected, you cannot say if your view will be removed or not. Let the control, button or segmentedControl do its work and if you want to add a badge, add it as a subview of its parent.
[surveyCountBadge setFrame:CGRectMake(105.0f+segmentedControl.frame.origin.x,
-10.0f+sementedControl.frame.origin.y, surveyCountBadge.frame.size.width,
surveyCountBadge.frame.size.height)];
[[segmentedControl superView] addSubview: surveyCountBadge];

problem with UIPopoverController while navigation

I have UIPopoverController and two ViewController class.
SaveProject and ProjectName is viewController class.
When i am clicking save project. its give me navigation and load another view.
But the UIPopoverController size height is getting increased to the total view size.
Can any one help me out with this..
I am attaching here code:
-(void)createSaveAndCloseView{
saveAndCloseViewController = [[WGSaveAndCloseViewController alloc]init];
[saveAndCloseViewController.view setFrame:CGRectMake(0, 0, 310, 171)];
saveAndCloseViewController.navigationItem.title = #"Save or Discard";
UIBarButtonItem *cancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStylePlain target:self action:#selector(cancelAction:)];
saveAndCloseViewController.navigationItem.rightBarButtonItem = cancel;
[cancel release];
UINavigationController *navControl = [[UINavigationController alloc] initWithRootViewController:saveAndCloseViewController];
saveAndClosePopupView = [[UIPopoverController alloc] initWithContentViewController:navControl];
saveAndClosePopupView.delegate = self;
saveAndClosePopupView.popoverContentSize = CGSizeMake(312, 160);
saveAndCloseViewController.view.contentMode = UIViewContentModeScaleAspectFill;
[saveAndClosePopupView presentPopoverFromRect:[saveAndClose bounds] inView:saveAndClose permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
[navControl release];
}
-(void)saveProjectClick:(id)sender{
if (saveProject.tag == tagSave) {
WGNameProjectViewController *nameProjectViewController = [[WGNameProjectViewController alloc] initWithNibName:nil bundle:nil];
[self.navigationController pushViewController:nameProjectViewController animated:YES];
nameProjectViewController.navigationItem.title = #"Name Project";
[nameProjectViewController release];
nameProjectViewController = nil;
}
}
You just need to add something like:
self.preferredContentSize = <view size>
to the viewWillAppear: method in each of your view controllers that are displayed in the popover.

Segment Button is not displaying on the navigation bar in iphone

Hello
I am having problem in displaying segmented button on the navigation bar
I try this code it's not working
I have the Tab Bar For calling this tabBar I use
This code TabBar is working fine but Button on the navigation bar is not displaying
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITabBarController *tabcontroller = [[UITabBarController alloc] init];
UITabBarController *vc1 = [[TMapViewController alloc] init];
UITabBarController *vc2 = [[TShareController alloc]init];
UITabBarController *vc3 = [[TSpeedometerController alloc]init];
UITabBarController *vc4 = [[TReviewsController alloc]init];
NSArray *viewControllers = [NSArray arrayWithObjects:vc1,vc2,vc3,vc4,nil];
[tabcontroller setViewControllers:viewControllers];
[vc1 release];
[vc2 release];
[vc3 release];
[vc4 release];
[self.navigationController pushViewController: tabcontroller animated: YES];
}
And right this code into the TMapViewController.m for calling
The button onto the navigation bar
-(id)init
{
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"Map"];
UIImage *i = [UIImage imageNamed:#"mapper_f.png"];
[tbi setImage:i];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"Activitieslist.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 50, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
return self;
}
Same thing i use into the
- (void)viewDidLoad {
[super viewDidLoad];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"Activitieslist.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 50, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
self.navigationItem.rightBarButtonItem = segmentBarItem;
[segmentBarItem release];
[[self.navigationBar topItem] setTitleView:segmentedControl];
[[[self.navigationBar topItem] titleView] setUserInteractionEnabled:YES];
TLocationManager *manager = [[TLocationManager alloc] init];
self.locationManager = manager;
self.locationManager.delegate = self;
[manager release];
TMapRoutes *routes = [[TMapRoutes alloc] init];
self.mapRoutes = routes;
self.mapRoutes.mapView = self.myMapView;
[routes release];
self.myMapView.delegate = self;
/* Run the simulator */
[locationManager startUpdatingLocation];
}
even it not work it give me same error .
Thanks in advance
Harish
Set it as [self.navigationBar titleView:segmentedControl]
-(void)loadView {
UITabBarItem *tbi = [self tabBarItem];
[tbi setTitle:#"Map"];
UIImage *i = [UIImage imageNamed:#"mapper_f.png"];
[tbi setImage:i];
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:
[UIImage imageNamed:#"Activitieslist.png"],
nil]];
[segmentedControl addTarget:self action:#selector(segmentAction:) forControlEvents:UIControlEventValueChanged];
segmentedControl.frame = CGRectMake(0, 0, 50, 35);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.momentary = YES;
UIBarButtonItem *segmentBarItem = [[UIBarButtonItem alloc] initWithCustomView:segmentedControl];
[segmentedControl release];
[[self.navigationBar topItem] setTitleView:segmentedControl]; [[[self.navigationBAr topItem] titleView] setUserInteractionEnabled:YES];
[segmentBarItem release];