IOS 8 Objective c UIBarButtonItem action doesnt work - objective-c

Im using UIBarButtonItem action and it works on ios 7 and not on ios 8
In ios 7 after clicking on the button the method cancelButtonPressed (which defined in action) is running but in ios8 nothing happend the cancelButtonPressed method doesnt called.
id cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelButtonPressed:)
];
Im using it in barcode scanner plugin, it is open the camera and in that screen i have the cancel button:
- (UIView*)buildOverlayView {
if ( nil != self.alternateXib )
{
return [self buildOverlayViewFromXib];
}
CGRect bounds = self.view.bounds;
bounds = CGRectMake(0, 0, bounds.size.width, bounds.size.height);
UIView* overlayView = [[[UIView alloc] initWithFrame:bounds] autorelease];
overlayView.autoresizesSubviews = YES;
overlayView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
overlayView.opaque = NO;
UIToolbar* toolbar = [[[UIToolbar alloc] init] autorelease];
toolbar.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
id cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelButtonPressed:)
];
//self.navigationItem.rightBarButtonItem=cancelButton;
id flexSpace = [[[UIBarButtonItem alloc] autorelease]
initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
target:nil
action:nil
];
#if USE_SHUTTER
id shutterButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCamera
target:(id)self
action:#selector(shutterButtonPressed)
];
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace,shutterButton,nil];
#else
toolbar.items = [NSArray arrayWithObjects:flexSpace,cancelButton,flexSpace,nil];
#endif
bounds = overlayView.bounds;
[toolbar sizeToFit];
CGFloat toolbarHeight = [toolbar frame].size.height;
CGFloat rootViewHeight = CGRectGetHeight(bounds);
CGFloat rootViewWidth = CGRectGetWidth(bounds);
CGRect rectArea = CGRectMake(0, rootViewHeight - toolbarHeight, rootViewWidth, toolbarHeight);
[toolbar setFrame:rectArea];
[overlayView addSubview: toolbar];
UIImage* reticleImage = [self buildReticleImage];
UIView* reticleView = [[[UIImageView alloc] initWithImage: reticleImage] autorelease];
CGFloat minAxis = MIN(rootViewHeight, rootViewWidth);
rectArea = CGRectMake(
0.5 * (rootViewWidth - minAxis),
0.5 * (rootViewHeight - minAxis),
minAxis,
minAxis
);
[reticleView setFrame:rectArea];
reticleView.opaque = NO;
reticleView.contentMode = UIViewContentModeScaleAspectFit;
reticleView.autoresizingMask = 0
| UIViewAutoresizingFlexibleLeftMargin
| UIViewAutoresizingFlexibleRightMargin
| UIViewAutoresizingFlexibleTopMargin
| UIViewAutoresizingFlexibleBottomMargin
;
[overlayView addSubview: reticleView];
return overlayView;
}
what is the alternative way for using it on ios8?

remove autorelease and initialize bar button like that
id cancelButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancelButtonPressed:)
];
self.navigationItem.rightBarButtonItem=cancelButton;

You might want to make a global variable in class defination or a property for cancelButton instead of using local variable. I guess arc is removing cancelButton from memory.

Related

UISearchBar inside a NavigationBar Get Deferenced When Keyboard Shows Up

I'm putting a search bar inside a navigation bar. here is the code
self.navigationBar =[[UINavigationBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
[self.view addSubview:self.navigationBar];
self.doneButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(doneButtonPressed)];
self.searchBar = [[UISearchBar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 64)];
self.searchBar.autocorrectionType = UITextAutocorrectionTypeNo;
self.searchBar.placeholder = #"Name of City";
self.searchBar.delegate = self;
self.searchController = [[UISearchDisplayController alloc]initWithSearchBar:self.searchBar contentsController:self];
self.searchController.delegate = self;
self.searchController.searchResultsDelegate = self;
self.searchController.searchResultsDataSource = self;
self.searchController.searchResultsTitle = #"Add Location";
self.searchController.displaysSearchBarInNavigationBar = YES;
self.searchController.navigationItem.rightBarButtonItems = #[self.doneButton];
self.navigationBar.items = #[self.searchController.navigationItem];
but the search bar behaves incorrectly.
I'm expecting it would look like this
I've fixed this by bring the navigationBar to front after setting searchBar as the first responder
[self.searchController setActive:YES animated:NO];
[self.searchController.searchBar becomeFirstResponder];
[self.view bringSubviewToFront:self.navigationBar]; //this is the line I add

text format trouble objective-c

need to make effect, that text is interrupted by another text/uiimageview, but cannot understand how does it works. For example i need to make interface similar to ios7 status bar where operator name such a "Oper ..." + icon + time. So i cannot do this right way
operatorName = [self getOperatorName];
UILabel *operatorLabel = [[UILabel alloc] init];
operatorLabel.backgroundColor = [UIColor clearColor];
operatorLabel.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:kStatusBarFontOperatorSize];
operatorLabel.textColor = kStatusBarTextColor;
operatorLabel.shadowOffset = CGSizeMake(0.f, -0.6);
operatorLabel.shadowColor = kStatusBarTextShadow;
operatorLabel.adjustsFontSizeToFitWidth = YES;
operatorLabel.text = operatorName;
operatorLabel.frame = CGRectMake(0.0, 0.0, operatorStrSize.width, operatorStrSize.height);
[operatorLabel sizeToFit];
/* connection type */
UIImageView *conImgView = [[UIImageView alloc] initWithImage:conImg];
/* time in status bar */
time = [self getStatusBarTime];
UILabel *statusBarLabel = [[UILabel alloc] init];
statusBarLabel.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:kStatusBarFontSize];
statusBarLabel.textColor = kStatusBarTextColor;
statusBarLabel.adjustsFontSizeToFitWidth = YES;
statusBarLabel.text = time;
int maxDistance = imgView.frame.size.width/2 - timeStrSize.width/2;
int connectionPower = 44;
double delimiter = 0;
NSString *cName = [self returnChoosenConnectionName];
if ([cName isEqualToString:#"Wi-Fi"]) {
delimiter = 6.5;
} else {
delimiter = 9.5;
}
int fullLine = connectionPower + operatorLabel.frame.size.width + delimiter + conImgView.frame.size.width;
if (fullLine > maxDistance) {
// need to interrupt text in operator name, but how ?
} else {
// all good placed
x = 44.0;
operatorLabel.frame = CGRectMake(x, 3.0, operatorStrSize.width , operatorStrSize.height);
[operatorLabel sizeToFit];
NSString *cName = [self returnChoosenConnectionName];
if ([cName isEqualToString:#"Wi-Fi"]) {
x += operatorLabel.frame.size.width + 6.5;
} else {
x += operatorLabel.frame.size.width + 9.5;
}
conImgView.frame = CGRectMake(x, 0.0, conImgView.frame.size.width, conImgView.frame.size.height);
[imgView addSubview:operatorLabel];
[imgView addSubview:conImgView];
}
statusBarLabel.frame = CGRectMake(imgView.frame.size.width/2 - timeStrSize.width/2, 2.5, timeStrSize.width , timeStrSize.height);
[imgView addSubview:statusBarLabel];
What i need:
what i have:
I would suggest to use autolayout rather than trying to calculate everything yourself. You may use XIB and set constraints in there or you may do everything programmatically. Here is a sample code that will get you started. It creates controls and sets constraints programatically; it doesn't use XIB.
- (void)viewDidLoad
{
[super viewDidLoad];
/*
Uncomment this if you would like to translate your subviews vertically.
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 40, self.view.frame.size.width, 50)];
[self.view addSubview:view];
UIView *superview = view;
*/
UIView *superview = self.view;
UILabel *label1 = [[UILabel alloc] init];
label1.font = [UIFont fontWithName:#"HelveticaNeue-Light" size:14.0];
label1.text = #"MTS";
[label1 sizeToFit];
[label1 setTranslatesAutoresizingMaskIntoConstraints:NO];
UILabel *label2 = [[UILabel alloc] init];
label2.font = [UIFont fontWithName:#"HelveticaNeue-Medium" size:14.0];
label2.text = #"1:22 PM";
[label2 sizeToFit];
[label2 setTranslatesAutoresizingMaskIntoConstraints:NO];
UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"strength.png"]];
UIImageView *image2 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"carrier.png"]];
[image1 setTranslatesAutoresizingMaskIntoConstraints:NO];
[image2 setTranslatesAutoresizingMaskIntoConstraints:NO];
[image1 sizeToFit];
[image2 sizeToFit];
[superview addSubview:image1];
[superview addSubview:label1];
[superview addSubview:image2];
[superview addSubview:label2];
NSDictionary *viewsDictionary = NSDictionaryOfVariableBindings(image1, image2, label1, label2);
NSString *format = [NSString stringWithFormat:#"|-[image1(<=%f)]-[label1]-[image2(<=%f)]-[label2(>=20)]-|",
image1.frame.size.width, image2.frame.size.width];
NSArray *constraints;
constraints = [NSLayoutConstraint constraintsWithVisualFormat:format
options:NSLayoutFormatAlignAllCenterY
metrics:nil
views:viewsDictionary];
[self.view addConstraints:constraints];
}
When adding views to a layout in code iOS will attempt to convert the autosizing mask for that view to auto layout constraints. Those auto-generated constraints will conflict with any constraints added within the application code. It is essential to turn the translation off:
setTranslatesAutoresizingMaskIntoConstraints:NO
The result of the code is:

UIBarbuttonItem disappears

I have the following function which displays the top bar on my app. It consists of an image and a UIBarButton. I call it from viewWillAppear. When I push the current controller and move to the next controller and then move Back to the main controller, the right bar button disappears. Please help.
-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
[rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
[rightButton release];
}
[titleImage release];
}
-(void)setTopBar
{
self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0.6367 green:0.6367 blue:0.6367 alpha:1.0];
UIImage *titleImage = [UIImage imageNamed: MAIN_TOPBAR];
if([self.navigationController.navigationBar respondsToSelector:#selector(setBackgroundImage:forBarMetrics:)] ) {
//iOS 5 new UINavigationBar custom background
[self.navigationController.navigationBar setBackgroundImage:titleImage forBarMetrics: UIBarMetricsDefault];
// NSLog(#"loading nav items");
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
rightButton.tag = 11111;
self.navigationItem.rightBarButtonItem = rightButton;
// [rightButton release];
}
else
{
CGRect titleFrame = self.navigationController.navigationBar.frame;
UIImageView *titleImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0.0, 0.0, titleFrame.size.width,titleFrame.size.height)];
[titleImageView setImage:titleImage];
[titleImageView setTag:TOPBAR_TAG];
// [self.navigationController.navigationBar sendSubviewToBack:titleImageView];
// self.navigationItem.titleView = titleImageView;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:#"More Apps" style:UIBarButtonItemStylePlain target:self action:#selector(loadFAADAds)];
self.navigationItem.rightBarButtonItem = rightButton;
//self.navigationController.navigationItem.rightBarButtonItem = rightButton;
[self.navigationController.navigationBar insertSubview:titleImageView atIndex:0];
[self.navigationController.navigationBar sendSubviewToBack:self.navigationController.navigationBar];
//[rightButton release];
}
// [titleImage release];
}

Navigation bar button is not clickable

I add this navigation bar code but i am not able to click on the left side lat item button "Edit" and when i replace with textbox even that is also not work (not take value), if you can figure that out what is the wrong with this code i will be very help full to you.
Thanks,
Aashish
find the screen shot
http://www.flickr.com/photos/71234685#N02/6440926473/in/photostream
-(void) initializeNavigationalBar {
UIToolbar* _leftNavBarTools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 300, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* _leftNavBarButtons = [[NSMutableArray alloc] initWithCapacity:4];
UIBarButtonItem* _editScenarioProduct = self.editButtonItem;
UIBarButtonItem* _addTurftype = [[UIBarButtonItem alloc] initWithTitle:#"Turftype" style:UIBarButtonItemStyleBordered target:self action:#selector(getTurfType)];
UIBarButtonItem *_addTargetN = [[UIBarButtonItem alloc] initWithTitle:#"Target N" style:UIBarButtonItemStyleBordered target:self action:#selector(getTargetNperWeek)];
UIBarButtonItem *_addMapRegion = [[UIBarButtonItem alloc] initWithTitle:#"Map Region" style:UIBarButtonItemStyleBordered target:self action:#selector(getMapRegion)];
CGRect _textFrame = CGRectMake(0, 0, 80, 30 );
_turfacresTextField = [[UITextField alloc] initWithFrame:_textFrame];
_turfacresTextField.borderStyle = UITextBorderStyleRoundedRect;
_turfacresTextField.font = [UIFont systemFontOfSize:12.0];
_turfacresTextField.placeholder = #"Turf Acres";
_turfacresTextField.textAlignment = UITextAlignmentCenter;
_turfacresTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
_turfacresTextField.autocorrectionType = UITextAutocorrectionTypeNo; // no auto correction support
_turfacresTextField.keyboardType = UIKeyboardTypeDefault;
_turfacresTextField.returnKeyType = UIReturnKeyDone;
UIBarButtonItem *_addTurfacres = [[UIBarButtonItem alloc] initWithCustomView:_turfacresTextField];
[_leftNavBarButtons addObject:_addTurftype];
[_leftNavBarButtons addObject:_addTargetN];
[_leftNavBarButtons addObject:_addMapRegion];
[_leftNavBarButtons addObject:_addTurfacres];
[_leftNavBarButtons addObject:_editScenarioProduct];
[_leftNavBarTools setItems:_leftNavBarButtons animated:NO];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_leftNavBarTools];
UIToolbar* _rightNavBarTools = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 200, 44.01)];
// create the array to hold the buttons, which then gets added to the toolbar
NSMutableArray* _rightNavBarButtons = [[NSMutableArray alloc] initWithCapacity:4];
UIBarButtonItem *_addSoilSetting = [[UIBarButtonItem alloc] initWithTitle:#"Settings" style:UIBarButtonItemStyleBordered target:self action:#selector(setSoilandCarryover)];
[_rightNavBarButtons addObject:_addSoilSetting];
UIBarButtonItem* _addApplication = [[UIBarButtonItem alloc] initWithTitle:#"Add Appplication" style:UIBarButtonItemStyleBordered target:self action:#selector(addApplication)];
[_rightNavBarButtons addObject:_addApplication];
[_rightNavBarTools setItems:_rightNavBarButtons animated:NO];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:_rightNavBarTools];
}
Try [self editButtonItem] instead of self.editButtonItem.

Bottom half of UITabbar is not responding to clicks

I have created a UITabbar with 3 items and placed it on a UIScrollView
When I click the buttons of the tabbar they do not respond in the bottom half.
The upper area is working fine.
When clicking in the area just above the tabbar the tabs are also switched.
What can be wrong?
How can i correct this misalignment of the clickable button area?
In viewDidLoad:
[super viewDidLoad];
scroll.frame = CGRectMake(0, 20, 320, 460);
scroll.pagingEnabled = YES;
scroll.contentSize = CGSizeMake(320 * 2, 460);
scroll.showsHorizontalScrollIndicator = NO;
scroll.showsVerticalScrollIndicator = NO;
scroll.scrollsToTop = NO;
scroll.delegate = self;
scroll.pagingEnabled = YES;
viewNavController1 = [[viewNavController1 alloc] init];
ctrl = [[UITabBarController alloc] init];
ViewController1 *viewC1= [[ViewController1 alloc] initWithNibName:#"ViewController1" bundle:nil];
UINavigationController *control = [[UINavigationController alloc] initWithRootViewController:viewC1];
viewC1.title = #"Title1";
[viewC1 release];
ViewController2 *viewC2 = [[ViewController2 alloc] initWithNibName:#"ViewController2" bundle:nil];
UINavigationController *control2 = [[UINavigationController alloc] initWithRootViewController:viewC2];
viewC2.title = #"Title2";
[viewC2 release];
UINavigationController *control3 = [[UINavigationController alloc] init];
ViewController3 *viewC3 = [[ViewController3 alloc] initWithNibName:#"ViewController3" bundle:nil];
[control3 pushViewController:viewC3 animated:NO];
viewC3.title = #"Title3";
[viewC3 release];
[ctrl setViewControllers:[NSArray arrayWithObjects:control,control2,control3,nil]];
CGRect frame = scroll.frame;
frame.origin.x = frame.size.width * 0;
frame.origin.y = 0;
viewNavController1.view.frame = frame;
viewC4 = [[ViewController4 alloc] initWithNibName:#"ViewController4" bundle:nil];
[viewNavController1 pushViewController:viewC4 animated:NO];
[scroll addSubview:viewNavController1.view];
frame = scroll.frame;
frame.origin.x = frame.size.width * 1;
frame.origin.y = 0;
ctrl.view.frame = frame;
[scroll addSubview:ctrl.view];
[scroll scrollRectToVisible:CGRectMake(320, 0, 320, 460) animated:NO];
UITabBarItem *itm = [ctrl.tabBar.items objectAtIndex:0];
itm.image = [UIImage imageNamed:#"img1.png"];
itm = [ctrl.tabBar.items objectAtIndex:1];
itm.image = [UIImage imageNamed:#"img2.png"];
itm = [ctrl.tabBar.items objectAtIndex:2];
itm.image = [UIImage imageNamed:#"img3.png"];
[control release];
[control2 release];
[control3 release];
The problem was that the UITabbarController did not have its view added as the root view of the windows. Which it apparently assumes.
so I had to trick it using:
[vc setWantsFullScreenLayout:YES];
vc is the main ViewController holding the scrollview containing the UITabbarController.
See Offset on UIWindow addSubview for more explanations.