I have an app with four viewControllers, and there is a lot of code that is repeating exactly the same way in the four view controllers, and I would like to know what is the best OOP way to write this code only one time and reuse it in the others view controllers.
This is the code from viewDidLoad of my first view controller:
- (void)viewDidLoad {
[super viewDidLoad];
[self setCanDisplayBannerAds:YES];
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setImage:[UIImage imageNamed:#"iconoBota30x30.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setImage:[UIImage imageNamed:#"actividades.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setImage:[UIImage imageNamed:#"nosotros.png"]];
float width = [UIScreen mainScreen].bounds.size.width;
float height = [UIScreen mainScreen].bounds.size.height;
static_iVar = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, width,height)];
static_iVar.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://guiasdelsur.es"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[static_iVar loadRequest:requestObj];
[self.view addSubview:static_iVar];
//GESTURES
//DOWN
UISwipeGestureRecognizer *gest1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(hideTabBar:)];
gest1.delegate = self;
[gest1 setDirection:UISwipeGestureRecognizerDirectionDown];
[self.view addGestureRecognizer:gest1];
//UP
UISwipeGestureRecognizer *gest2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(showTabBar:)];
gest2.delegate = self;
[gest2 setDirection:UISwipeGestureRecognizerDirectionUp];
[self.view addGestureRecognizer:gest2];
//first tab
_tabInicio = [[UITabBarItem alloc] initWithTitle:#"Inicio" image:[UIImage imageNamed:#"d.png"] tag:0];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
[self.tabBarController setSelectedIndex:0];
self.tabBarItem = _tabInicio;
_cToolBar = [[UIToolbar alloc] init];
_cToolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[_cToolBar setBarStyle:UIBarStyleBlack];
[_cToolBar setTranslucent:YES ];
[_cToolBar setTintColor:[UIColor greenColor]];
// Do any additional setup after loading the view, typically from a nib.
NSString *backArrowString = #"\U000025C0\U0000FE0E AtrĂ¡s"; //BLACK LEFT-POINTING TRIANGLE PLUS VARIATION SELECTOR
UIBarButtonItem *back = [[UIBarButtonItem alloc] initWithTitle:backArrowString style:UIBarButtonItemStyleDone target:nil action:#selector(goBack)];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:#selector(refreshControl)];
[right setTintColor:[UIColor greenColor]];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[items addObject:back];
[items addObject:flexibleSpace];
[items addObject:right];
[_cToolBar setItems:items animated:NO];
[self.view addSubview:_cToolBar];
//iAD
ADBannerView * adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, 44);
adView.delegate = self;
[self.view addSubview:adView];
//loading indicator
_indicator = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_indicator setCenter:self.view.center];
[_indicator setHidesWhenStopped:YES];
[self.view addSubview:_indicator];
[_indicator startAnimating];
}
And this is the code from viewDidLoad method of my secondViewController, very similar to the first one. The differences in all my view controllers are the same than those ones.
- (void)viewDidLoad {
[super viewDidLoad];
[self setCanDisplayBannerAds:YES];
float width = [UIScreen mainScreen].bounds.size.width;
float height = [UIScreen mainScreen].bounds.size.height;
static_iVar2 = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, width,height)];
static_iVar2.delegate = self;
NSURL *url = [NSURL URLWithString:#"http://guiasdelsur.es/category/programas/"];
NSURLRequest *requestObj = [NSURLRequest requestWithURL:url];
[static_iVar2 loadRequest:requestObj];
[self.view addSubview:static_iVar2];
//second tab
_secondTab = [[UITabBarItem alloc] initWithTitle:#"Programas" image:[UIImage imageNamed:#"iconoBota30x30.png"] tag:1];
[[UITabBar appearance] setSelectedImageTintColor:[UIColor greenColor]];
[self.tabBarController setSelectedIndex:1];
self.tabBarItem = _secondTab;
//GESTURES
//DOWN
UISwipeGestureRecognizer *gest1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(hideTabBar:)];
gest1.delegate = self;
[gest1 setDirection:UISwipeGestureRecognizerDirectionDown];
[self.view addGestureRecognizer:gest1];
//UP
UISwipeGestureRecognizer *gest2 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(showTabBar:)];
gest2.delegate = self;
[gest2 setDirection:UISwipeGestureRecognizerDirectionUp];
[self.view addGestureRecognizer:gest2];
UIToolbar *cToolBar = [[UIToolbar alloc] init];
cToolBar.frame = CGRectMake(0, 0, self.view.frame.size.width, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[cToolBar setBarStyle:UIBarStyleBlack];
[cToolBar setTranslucent:YES ];
[cToolBar setTintColor:[UIColor greenColor]];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil ];
UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:nil action:#selector(refreshControl)];
[right setTintColor:[UIColor greenColor]];
[items addObject:flexibleSpace];
[items addObject:right];
[cToolBar setItems:items animated:NO];
[self.view addSubview:cToolBar];
//iAD
ADBannerView * adView = [[ADBannerView alloc] initWithFrame:CGRectZero];
adView.frame = CGRectOffset(adView.frame, 0, 44);
adView.delegate = self;
[self.view addSubview:adView];
//Indicator
_indicador = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleGray];
[_indicador setCenter:self.view.center];
[_indicador setHidesWhenStopped:YES];
[self.view addSubview:_indicador];
[_indicador startAnimating];
}
It is a very good idea to refactor this code; it is an example of DRY. The best way in this case would be to use inheritance: write all common code in the ViewController class, e.g.
ViewController.h
#interface ViewController : UIViewController {
// Variables which are declared here can also be used in the subclasses
}
ViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
[self setCanDisplayBannerAds:YES];
// more common code
...
}
and have your other view controllers inherit from ViewController (rather than UIViewController):
FirstViewController.h
#interface FirstViewController : ViewController
...
FirstViewController.m
- (void)viewDidLoad {
[super viewDidLoad];
// more specific code
[[[self.tabBarController.viewControllers objectAtIndex:1] tabBarItem]setImage:[UIImage imageNamed:#"iconoBota30x30.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:2] tabBarItem]setImage:[UIImage imageNamed:#"actividades.png"]];
[[[self.tabBarController.viewControllers objectAtIndex:3] tabBarItem]setImage:[UIImage imageNamed:#"nosotros.png"]];
...
}
Related
I have an app that uses the UIImagePickerController to take a picture. The problem is that I only want the camera option to be available and I understand that I need to hide the standard controls:
cameraUI.showsCameraControls=NO;
and use a cameraOverlayView to provide my own controls. I have had a look at Apple's PhotoPicker project already and my initial problem is how do I get an Overlay object onto my storyboard? I can't find such an object in the library.
Here is the code :
toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height-54, self.view.frame.size.width, 55)];
toolBar.barStyle = UIBarStyleBlackOpaque;
NSArray *items=[NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelPicture)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(shootPicture)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
nil];
[toolBar setItems:items];
// create the overlay view
overlayView = [[OverlayView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-44)];
// important - it needs to be transparent so the camera preview shows through!
overlayView.opaque=NO;
overlayView.backgroundColor=[UIColor clearColor];
// parent view for our overlay
UIView *cameraView=[[UIView alloc] initWithFrame:self.view.bounds];
[cameraView addSubview:overlayView];
[cameraView addSubview:toolBar];
imagePickerController = [[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == NO){
NSLog(#"Camera not available");
return;
}
imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePickerController.delegate = self;
// hide the camera controls
imagePickerController.showsCameraControls=NO;
imagePickerController.wantsFullScreenLayout = YES;
[imagePickerController setCameraOverlayView:cameraView];
[self presentViewController:imagePickerController animated:YES completion:nil];
Declare this in your header file :
UIImagePickerController * imagePickerController;
UIToolbar *toolBar;
OverlayView *overlayView;
Add this OverlayView.h and .m Class from Apples PhotoPicker.
Actions for capturing photo using custom camera button:
-(void) shootPicture {
[imagePickerController takePicture];
}
- (IBAction)cancelPicture {
[self dismissViewControllerAnimated:YES completion:nil];
}
Output will come like this below attached screenshot (I have added a capture button and cancel button in custom overlay view):
Happy Coding :)
Here is my code. If you want the camera to appear as soon as the view controller opens, make sure you initialize the UIImagePickerController in viewDidAppear like I did (viewDidLoad does not work).
#interface CameraViewController ()
#property UIImagePickerController *PickerController;
#property CGFloat HeightOfButtons;
#end
- (UIView *)createCustomOverlayView
{
// Main overlay view created
UIView *main_overlay_view = [[UIView alloc] initWithFrame:self.view.bounds];
// Clear view (live camera feed) created and added to main overlay view
// ------------------------------------------------------------------------
UIView *clear_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - self.HeightOfButtons)];
clear_view.opaque = NO;
clear_view.backgroundColor = [UIColor clearColor];
[main_overlay_view addSubview:clear_view];
// ------------------------------------------------------------------------
// Creates two red buttons on the bottom of the view (on top of the live camera feed)
// Then adds the buttons to the main overlay view
// You can, of course, customize these buttons however you want
// ------------------------------------------------------------------------
for(int i = 0; i < 2; i++) {
self.HeightOfButtons = 100;
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
// when a button is touched, UIImagePickerController snaps a picture
[button addTarget:self action:#selector(testIfButtonResponds) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake( i * self.view.frame.size.width / 2, self.view.frame.size.height - self.HeightOfButtons, self.view.frame.size.width / 2, self.HeightOfButtons);
[button setBackgroundColor:[UIColor redColor]];
[main_overlay_view addSubview:button];
}
// ------------------------------------------------------------------------
return main_overlay_view;
}
- (void)makeCustomCameraAppear
{
self.PickerController = [[UIImagePickerController alloc] init];
self.PickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
self.PickerController.showsCameraControls = NO;
self.PickerController.delegate = self;
UIView *overlay_view = [self createCustomOverlayView];
[self.PickerController setCameraOverlayView:overlay_view];
[self presentViewController:self.PickerController animated:YES completion:NULL];
}
- (void)viewDidAppear:(BOOL)animated
{
[self makeCustomCameraAppear];
}
great information. To add a little to what Raghu did:
up top:
UIImagePicker *_imagePicker;
//inside your take a picture method
UIToolbar *toolBar=[[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 100)];
toolBar.barStyle = UIBarStyleBlackOpaque;
NSArray *items=[NSArray arrayWithObjects:
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelPicker)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(shootPicture)],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
nil];
[toolBar setItems:items];
toolBar.backgroundColor=[UIColor blackColor];
UIImageView *tp=[[UIImageView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-100, self.view.frame.size.width, 100)];
tp.userInteractionEnabled=YES;
tp.backgroundColor=[UIColor blackColor];
[tp addSubview:toolBar]; //add the toolbar to the uiimageview
_imagePicker=[[UIImagePickerController alloc]init];
_imagePicker.sourceType=UIImagePickerControllerSourceTypeCamera;
_imagePicker.delegate = self;
_imagePicker.showsCameraControls=NO; //we will make custom controls.
[_imagePicker.view addSubview:tp];
-(void)cancelPicker{
//get rid of the image picker
[self dismissViewControllerAnimated:YES completion:nil]; //dismiss uiimagepicker
_imagePicker=nil;
}
//take the picture, it will then go directly to - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info method
-(void)shootPicture{
[_imagePicker takePicture];
}
In my iPad app, i add navigation bar and tool bar manually.
I also add scroll view and image view by coding.
My problem is i can see both navigation bar and tool bar.
I can click button on navigation bar.
But, i can't click bar button on tool bar.
How can i do it?
This is my code.
- (void)viewDidLoad
{
[super loadView];
self.view.backgroundColor = [UIColor grayColor];
UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];
ScrollView.pagingEnabled = YES;
NSInteger numberOfViews = 4;
for (int i = 0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.view.frame.size.width;
// Create a UIImage to hold Info.png
UIImage *image1 = [UIImage imageNamed:#"Image-001.jpg"];
UIImage *image2 = [UIImage imageNamed:#"Image-002.jpg"];
UIImage *image3 = [UIImage imageNamed:#"Image-003.jpg"];
UIImage *image4 = [UIImage imageNamed:#"Image-004.jpg"];
NSArray *images = [[NSArray alloc] initWithObjects:image1,image2,image3,image4,nil];
UIImageView *ImageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.view.frame.size.width, self.view.frame.size.height-88)];
[ImageView setImage:[images objectAtIndex:i]];
[ScrollView addSubview:ImageView];
}
ScrollView.contentSize = CGSizeMake(self.view.frame.size.width * numberOfViews, self.view.frame.size.height);
[self.view addSubview:ScrollView];
}
I dont Know what is your exact problem, Because your displayed code is not related to UIToolBar.
But Following code i added two UIBarButton with flexSpace.
you can add UIBarButton as you want
UIToolbar *Toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
Toolbar = UIBarStyleBlackTranslucent;
[Toolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
[flexSpace release];
UIBarButtonItem *btnCancel = [[UIBarButtonItem alloc] initWithTitle:#"Cancel" style:UIBarButtonItemStyleBordered target:self action:#selector(Cancel)];
[barItems addObject:btnCancel];
UIBarButtonItem *btnDone = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleBordered target:self action:#selector(done)];
[barItems addObject:btnDone];
[Toolbar setItems:barItems animated:YES];
Following Method is call when tapped on bar Button
-(void)Cancel
{
// Write Code for Cancel Method
}
-(void)done
{
// Write Code for Done Method
}
This code might helpful in your own case.
Thanks :)
Thanks everybody. I can solve my problem.
I change my code.
UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height)];
To this.
UIScrollView *ScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 44, self.view.frame.size.width, self.view.frame.size.height-88)];
i have created a toolbar as below shown code and now i dont knw how to add the items to it.
- (void)viewDidLoad
{
[super viewDidLoad];
UIToolbar *toolbar = [[UIToolbar alloc] init];
toolbar.frame = CGRectMake(0, 418, 350, 44);
NSMutableArray *items = [[NSMutableArray alloc] init];
[items addObject:[[[UIBarButtonItem alloc] init] autorelease]];
[toolbar setItems:items animated:YES];
[items release];
[self.view addSubview:toolbar];
[toolbar release];
}
Try this code
UIBarButtonItem *customItem = [[UIBarButtonItem alloc] initWithTitle:#"Item" style:UIBarButtonItemStyleBordered target:self action:#selector(action:)];
NSArray *items = [NSArray arrayWithObjects: customItem, nil];
[toolbar setItems:items animated:NO];
[customItem release];
You can also refer to this link:
How to create this toolbar programmatically
Please do google search and Stack Overflow search before posting.
use
[self setToolbarItems: buttons animated:NO];
as u want to set toolbaritems and uiviewcontroller has toolbar itself.
I have tested my app using instrument and it show the following leak.
When I double click on the CameraVC in the stack trace it refer to the following line into my code.
this happen when I call the camera, I call it using the following code:-
- (IBAction) getCamera
{
// set up our camera overlay view
// tool bar - handy if you want to be able to exit from the image picker...
UIToolbar *toolBar=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 480-44, 320, 44)];
UIBarButtonItem *spaceItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem *spaceItem1 = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil] autorelease];
UIBarButtonItem *cancelItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:#selector(cancelPickingImag)] autorelease];
UIBarButtonItem *cameraItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:#selector(finishedAugmentedReality)] autorelease];
spaceItem.width = 2.0;
spaceItem1.width = 55.0;
NSArray *items=[NSArray arrayWithObjects:spaceItem,cancelItem,spaceItem1,cameraItem,nil];
[toolBar setItems:items];
// create the overlay view
overlayView=[[OverlayView alloc] initWithFrame:CGRectMake(0, 300, 320, 480-44)];
// important - it needs to be transparent so the camera preview shows through!
overlayView.opaque=NO;
overlayView.backgroundColor=[UIColor clearColor];
// parent view for our overlay
UIView *parentView=[[UIView alloc] initWithFrame:CGRectMake(0,0,320, 480)];
[parentView addSubview:overlayView];
[parentView addSubview:toolBar];
[parentView addSubview:lbl];
[parentView addSubview:overlayGraphicView];
// configure the image picker with our overlay view
//UIImagePickerController *picker=[[UIImagePickerController alloc] init];
if ([UIImagePickerController isSourceTypeAvailable:
UIImagePickerControllerSourceTypeCamera])
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
// hide the camera controls
imagePicker.showsCameraControls=NO;
imagePicker.delegate = self;
//imagePicker.allowsImageEditing = NO;
// and put our overlay view in
imagePicker.cameraOverlayView=parentView;
}
else
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.mediaTypes = [NSArray arrayWithObjects:
(NSString *) kUTTypeImage,
nil];
imagePicker.allowsEditing = NO;
}
[self presentModalViewController:imagePicker animated:YES];
//Free memory
[imagePicker release];//,imagePicker =nil;
[parentView release], parentView=nil;
[overlayGraphicView release], overlayGraphicView= nil;
[lbl release], lbl=nil;
[overlayView release];//, overlayView =nil;
[toolBar release], toolBar=nil;
}
any help is highly appreciated
Thanks
parentView is being created with [alloc [init...]], meaning it's retained. Then you assign it to the cameraOverlayView property of imagePicker, meaning it gets retained again. You need to release it after you do that assignment.
(This is almost certainly the cause of the "leak", but would not cause a "crash". You say you're having a crash but don't describe that at all.)
I have one .xib in which I have a view that has a UISegmentedControl and I am adding them programmatically.
In appdelegate I have written the code for adding toolbar and there's event.
On toolbar click the UISegmentedControl is loaded from the given mutablearray.
What I want to do is: when I click on toolbar button the value of UISegmentedControl cannot be changed.
I have written a method for adding UISegment and its value. Every time on toolbar button click, I call the method.
Method for creating UISegmentedControl:
delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
NSMutableArray *itemArray = [[NSMutableArray alloc] init];
itemArrayforID = [[NSMutableArray alloc] init];
itemArrayforImage = [[NSMutableArray alloc] init];
bool isfirst = true;
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
[segmentedControl removeFromSuperview];
int temp =0;
NSLog(#"row count : %d",delegate.TopMenus.count);
for(int i=0;i<delegate.TopMenus.count;i++)
{
delegate.curTopsMenu = [delegate.TopMenus objectAtIndex:i];
NSLog(#"toolbar click : %#",delegate.toolbarbtnclick);
NSLog(#"parent_id : %#",[delegate.curTopsMenu valueForKey:#"parent_id"]);
if([delegate.toolbarbtnclick isEqualToString:[delegate.curTopsMenu valueForKey:#"parent_id"]])
{
int aaa = [[delegate.curTopsMenu valueForKey:#"top_menu_id"] intValue];
if(isfirst)
{
MenuLoadID = [NSString stringWithFormat:#"%d",aaa];
isfirst =false;
}
NSString *TEXT = [delegate.curTopsMenu valueForKey:#"top_menu_text"];
NSString *TEXTID = [delegate.curTopsMenu valueForKey:#"top_menu_id"];
NSString *SelectIMG = [delegate.curTopsMenu valueForKey:#"image_path"];
NSLog(#" TEXT : %# and TEXTID : %#",TEXT,TEXTID);
[itemArray insertObject:TEXT atIndex:temp];
[itemArrayforID insertObject:TEXTID atIndex:temp];
[itemArrayforImage insertObject:SelectIMG atIndex:temp];
temp++;
}
}
segmentedControl = [[UISegmentedControl alloc] initWithItems:itemArray];
[segmentedControl removeFromSuperview];
segmentedControl.frame = CGRectMake(0, 100, 320, 40);
segmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
segmentedControl.selectedSegmentIndex = 0;
[segmentedControl addTarget:self action:#selector(changeSegment:) forControlEvents:UIControlEventValueChanged];
UIImage *segmentSelected = [[UIImage imageNamed:#"games-on.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
UIImage *segmentUnselected = [[UIImage imageNamed:#"games-ho.png"]
resizableImageWithCapInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
[[UISegmentedControl appearance] setBackgroundImage:segmentUnselected
forState:UIControlStateNormal barMetrics:UIBarMetricsDefault];
[[UISegmentedControl appearance] setBackgroundImage:segmentSelected
forState:UIControlStateSelected barMetrics:UIBarMetricsDefault];
[self.navigationController.view addSubview:segmentedControl];
[segmentedControl release];
segmentSelected = nil;
[itemArray release];
[self MenuRowCount];
Code for toolbar button click:
ViewController *VC = [[ViewController alloc] init];
[VC LoadTopMenuFromBottmClick];
Your question could be more clear, but here's my best guess at an answer. This assumes the UISegmentedControl is a property called yourSegmentedControl and has been allocated (preferably by a proper getter).
Update enabled/disabled:
[self.yourSegmentedControl setEnabled:aBOOL forSegmentAtIndex:anInt];
Update title:
[self.yourSegmentedControl setTitle:aString forSegmentAtIndex:anInt];
That said, your code could be simpler. If you put the UISegmentedControl in the UIViewController rather than the appDelegate you can take advantage of using the toolbarItems property to attach the control to the navigation controller. This next code isn't exactly your question, but may be instructive:
#interface StocksTableViewController()
#property (nonatomic, strong) IBOutlet UISegmentedControl *stockFilterSegmentedControl;
#end
#implementation StocksTableViewController
#synthesize stockFilterSegmentedControl = _stockFilterSegmentedControl;
- (UISegmentedControl*)stockFilterSegmentedControl {
if (!_stockFilterSegmentedControl) {
_stockFilterSegmentedControl = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:#"All",#"Holdings", #"To Do", nil]];
[_stockFilterSegmentedControl addTarget:self action:#selector(stockFilterControlPressed:) forControlEvents:UIControlEventValueChanged];
_stockFilterSegmentedControl.selectedSegmentIndex = 0;
_stockFilterSegmentedControl.autoresizingMask = UIViewAutoresizingFlexibleHeight;
_stockFilterSegmentedControl.segmentedControlStyle = UISegmentedControlStyleBar;
}
return _stockFilterSegmentedControl;
}
- (NSArray*)navFooterToolbarArray {
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.stockFilterSegmentedControl];
UIBarButtonItem *flexibleSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
UIBarButtonItem *refresh = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:#selector(refresh:)];
return [NSArray arrayWithObjects:flexibleSpace, barButtonItem, flexibleSpace, refresh, nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
self.toolbarItems = [self navFooterToolbarArray];
}
Enjoy,
Damien