View Controller hides after showing when called from ActionSheet manually - ios

I am writing a code, where another view controller is called from Action Sheet. I am using "performSegueWithIdentifier" for this because I want to pass different text depending on which action button is pressed. It was working fine but now as I call the view controller from Action sheet, it appears and then hides suddenly. I checked my code but can't figure out the bug. Here is my code. Note: I am not using navigation controller.
//"selectEditor" is the button action used for calling action sheet.
- (IBAction)selectEditor:(id)sender {
_sheet = [[UIActionSheet alloc] initWithTitle:#"Select type of Editor"
delegate:self
cancelButtonTitle:#"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:#"Button1",#"Button2", nil];
[_sheet showInView:self.view];
}
//Perform segue to vc when action button is pressed. "editorSegue" is segue identifier.
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
//trigger call here
if (buttonIndex==0)
{
EditorFlag=1;
[self performSegueWithIdentifier:#"editorSegue" sender:self];
}
if (buttonIndex==1)
{
EditorFlag=0;
[self performSegueWithIdentifier:#"editorSegue" sender:self];
}
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
if ([segue.identifier isEqualToString:#"editorSegue"]) {
//Additional code here. EditorFlag is used for identification to set different text on same view controller when different action buttons are pressed.
if (EditorFlag==1)
{
//EditTextController is the view controller which hides after showing
EditTextController *textEditor = [segue destinationViewController];
textEditor.textstring=#"some text";
}
else
{
EditTextController *textEditor = [segue destinationViewController];
textEditor.textstring=#"some text";
}
}
}
//Here is Editor text code, I am adding navigation bar manually but I also checked it by commenting all code in it.
#implementation EditTextController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
[[self navigationController] setNavigationBarHidden:YES animated:YES];
UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0,0, self.view.frame.size.width, 50)];
navBar.barTintColor = [UIColor whiteColor];
[self.view addSubview:navBar];
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithTitle:#"back" style:UIBarButtonItemStylePlain target:self action:#selector(back:)];
UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:_navString];
[navItem setLeftBarButtonItem:doneItem animated:YES];
UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:[UIImage imageNamed:#"Cloud_Arrow_Up.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(methodAction:) forControlEvents:UIControlEventTouchUpInside];
[button setFrame:CGRectMake(0, 0, 30, 30)];
navItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:button];
[navBar setItems:[NSArray arrayWithObject:navItem] animated:YES];
[self.textEditor setText:self.textstring];
_textEditor.font = [UIFont fontWithName:#"HelveticaNeue" size:24];
_textEditor.textColor = [UIColor blueColor];
}
-(void)back:(id)sender
{
//[self performSegueWithIdentifier:#"realTimeSegue" sender:sender];
}
- (void)methodAction:(UIButton *)button
{
//[self performSegueWithIdentifier:#"syncSegue" sender:button];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}

Related

Objective c i want to hide my navigationbar but i want to show navigationItem's rightBarButtonItem

i am new in objective-c .I am trying to hide navigationbar i used this code and it's work perfectly but problem is this when i hide navigationbar after i am failed to show navigationItem's rightBarButtonItem
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
self.navigationController.navigationBarHidden = YES;
// self.navigationItem.rightBarButtonItem.enabled = NO;
}
i am used this code for navigationItem
-(void)loadBackButton{
/*UIBarButtonSystemItemDone */
buttonItem = [[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismiss:)];
self.navigationItem.rightBarButtonItem = buttonItem;
}
- (void)dismiss:(id)sender {
NSLog(#"Dismis Done");
[self dismissModalViewControllerAnimated:YES];
//[self.view removeFromSuperview];
}
and my - (void)viewDidLoad are bellow
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor whiteColor];
//for load navigationItem's
[self loadBackButton];
self.navigationItem.rightBarButtonItem.enabled = YES;
// Do any additional setup after loading the view from its nib.
}
Hiding NavigationBar will hide your rightBarButtonItem too. One of the possible solutions is that you make the Navigation Bar transparent. You can use following code to do so:
[self.navigationController.navigationBar setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
self.navigationController.navigationBar.shadowImage = [UIImage new];
self.navigationController.navigationBar.translucent = YES;
self.navigationController.view.backgroundColor = [UIColor clearColor];

Hide/Show back button on Navigation Bar when Search Bar opens/closes

So I have a navigation bar that has a "Back" button on it and a UISearchBar to the right of this:
![enter image description here][1]
When the UISearchBar opens/shows, the cancel button hides/shows:
![enter image description here][2]
What I want is when the UISearchBar opens, I want it to basically "cover" the back button. When it closes, I want it so "uncover" the back button. Here is my code so far:
#import "SearchViewController.h"
#interface SearchViewController ()
#end
- (void)viewDidLoad {
[super viewDidLoad];
if ([self respondsToSelector:#selector(edgesForExtendedLayout)])
self.edgesForExtendedLayout = UIRectEdgeNone;
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = NO;
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
}
#end
What I've tried was to do: self.navigationItem.hidesBackButton = NO/YES; in the searchBarTextDidBeginEditing/searchBarTextDidEndEditing but this leaves the spot where the back button was empty:
![enter image description here][3]
Is there a way I can have the search bar extend over the back button? Thanks!
Try to do it by
self.navigationItem.leftBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
or
self.navigationItem.backBarButtonItem=nil;
Updated code :
#interface SearchViewController ()
{
UIBarButtonItem *backButtonItem;
UIBarButtonItem *negativeSpacer;
}
#end
#implementation SearchViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
UISearchBar *searchBar = [UISearchBar new];
searchBar.showsCancelButton = NO;
[searchBar sizeToFit];
searchBar.delegate = self;
self.navigationItem.titleView = searchBar;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:nil];
self.navigationItem.backBarButtonItem=nil;
self.navigationItem.hidesBackButton=YES;
UIButton *backButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 70.0f, 21.0f)];
UIImage *backImage = [UIImage imageNamed:#"Back.png"];
[backButton setImage:backImage forState:UIControlStateNormal];
[backButton setTitleEdgeInsets:UIEdgeInsetsMake(10.0, 10.0, 10.0, 0.0)];
[backButton setTitle:#"Back" forState:UIControlStateNormal];
[backButton addTarget:self action:#selector(backButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
backButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
negativeSpacer = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFixedSpace target:nil action:nil];
[negativeSpacer setWidth:-15];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:YES animated:YES];
self.navigationItem.leftBarButtonItems = nil;
}
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
[searchBar setShowsCancelButton:NO animated:YES];
}
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
[searchBar resignFirstResponder];
[searchBar setShowsCancelButton:NO animated:YES];
self.navigationItem.leftBarButtonItems = [NSArray arrayWithObjects:negativeSpacer,backButtonItem,nil];
}
- (IBAction)backButtonPressed:(id)sender{
[self.navigationController popViewControllerAnimated:YES];
}

Replace custom back button with default

There is a lot of questions about how to replace the default back button with a custom back button, but none that I know of about how to replace a custom back button with the default.
I have a web view that when [self.webView canGoBack] == YES, a custom back button appears. But after you go back all the way, that custom back button is still there, rather than the default. Is there something I can do to replace my custom back button with the default when [self.webView canGoBack] == NO?
Here is my relevant code:
#interface MerchViewController () <UIWebViewDelegate>
#property UIWebView *webView;
#end
#implementation MerchViewController
- (instancetype)init
{
self = [super init];
if (self) {
self.navigationItem.title = #"Merchandise";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
UIWebView *webView = [[UIWebView alloc] init];
webView.scalesPageToFit = YES;
self.view = webView;
self.webView = webView;
webView.delegate = self;
[self setURL];
}
- (void)updateBackButton {
if ([self.webView canGoBack]) {
{
[self.navigationItem setHidesBackButton:YES animated:NO];
//UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backTickets"] style:UIBarButtonItemStylePlain target:self action:#selector(backWasClicked:)];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] backButtonWith:#"Back" tintColor:[UIColor orangeColor] target:self andAction:#selector(backWasClicked:)];
backItem.tintColor = [UIColor orangeColor];
[self.navigationItem setLeftBarButtonItem:backItem animated:NO];
}
}
else {
/*
[self.navigationItem setHidesBackButton:YES animated:NO];
//UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"backTickets"] style:UIBarButtonItemStylePlain target:self action:#selector(backWasClicked:)];
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] backButtonWith:#"Fan Zone" tintColor:[UIColor orangeColor] target:self andAction:#selector(openMenu:)];
backItem.tintColor = [UIColor orangeColor];
[self.navigationItem setLeftBarButtonItem:backItem animated:NO];
*/
}
}
- (void)webViewDidStartLoad:(UIWebView *)webView {
[self updateBackButton];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
[self updateBackButton];
[_spinner stopAnimating];
}
- (void)backWasClicked:(id)sender {
if ([self.webView canGoBack]) {
[self.webView goBack];
}
}
- (void)setURL
{
NSURLRequest *req = [NSURLRequest requestWithURL:[NSURL URLWithString:#"https://www.lawlorscustom.com/omaha-lancers-hockey"]];
NSLog(#"loadRequest: %#", req);
[(UIWebView *)self.view loadRequest:req];
}
#end
The "else" portion in "updateBackButton" is commented out because if I don't comment it out, when the view first loads there are two back buttons overlapping.
After struggling for a while, the answer was to do:
[self.navigationItem setHidesBackButton:NO animated:NO];
self.navigationItem.leftBarButtonItem = self.navigationItem.backBarButtonItem;

Switch view to another view on storyboard

I am new in iOS programming.
What I am trying to do is:
I have some views in a storyboard and I'd like to switch between the views programatically. For example, when a button is clicked, I call a method and I want to change views here (I can call the method successfully). The buttons are also created programatically in different positions.
I have searched and I think it happens with NavigationController. I have a navigation controller which I created like so: menu Editor -> Embed In -> NavigationController. How could I do this using this NavigationController?
#Madhu and #Dilip ,I found a solution with xib filed class
icerik *secondViewController = [[icerik alloc] initWithNibName:#"icerik" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:secondViewController];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
navigationController.topViewController.title = #"SecondViewController";
//[self presentModalViewController:navigationController animated:YES];
if([self respondsToSelector:#selector(presentViewController:animated:completion:)])
[self presentViewController:navigationController animated:YES completion:^{/* done */}];
else if([self respondsToSelector:#selector(presentViewController:animated:)])
[self presentModalViewController:navigationController animated:YES];
I have a class with xib file named icerik, I solved it like this. It is opening but when I want to turn back, What can I do it ?
You can create btn using this code:
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button addTarget:self action:#selector(aMethod) forControlEvents:UIControlEventTouchDown];
[button setTitle:#"Show View" forState:UIControlStateNormal];
button.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
[self.view addSubview:button];
and for going to another vc use this code,if you want navigation bar:
-(void)aMethod
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:SecondViewController];
navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
navigationController.topViewController.title = #"SecondViewController";
[self presentModalViewController:navigationController animated:YES];
}
Else use this code:
-(void)aMethod
{
SecondViewController *secondViewController = [[SecondViewController alloc] initWithNibName:#"SecondViewController" bundle:nil];
[self presentModalViewController:secondViewController animated:YES];
}
And for come back to frist vc fromm second vc add this code in second vc.
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *closeButton = [[UIBarButtonItem alloc] initWithTitle:#"Back" style:UIBarButtonItemStyleBordered target:self action:#selector(backAction:)];
self.navigationItem.leftBarButtonItem = closeButton;
}
- (void)backAction:(id)sender {
[self dismissModalViewControllerAnimated:NO];
}
If your new to Objective-c first go with Views/ViewControllers. i.e. use addSubView property of UIView
UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(10, 0, 250.0, kCCCellHeaderHeight)];
[subView setBackgroundColor:[UIColor redcolor]];
[self.view addSubview:subView];
If your little known of UINavigationCOntroller Use pushViewController
CCFilteredVideosController *filteredController = [[CCFilteredVideosController alloc] initWithNibName:#"CCFilteredVideosController" bundle:nil];
[self.navigationController pushViewController:filteredController animated:YES];
[filteredController release];

Displaying WEPopover from UIButton instead of UIBarButtonItem

I am using WEPopover in my app to pop up a popover controller containg some buttons,it works when i put wepopover controller action in tabbarIteam but i need to get popover when i click UIButton.How to do this this is my barbuttonitem code for displaying popover.
-(IBAction)_clickbtnAccount:(id)sender
{
if (!self.popoverController)
{
UIViewController *contentViewController = [[pageAccount alloc]initWithNibName:#"pageAccount" bundle:nil];
self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease];
self.popoverController.delegate = self;
self.popoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];
[self.popoverController presentPopoverFromBarButtonItem:sender
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown)
animated:YES];
[contentViewController release];
}
else
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
-(IBAction)_clickbtnAccount:(id)sender
{
if (!self.popoverController)
{
UIButton *senderButton = (UIButton *)sender;
[self.popoverController presentPopoverFromRect:[senderButton frame]
inView:#"pageAccount"
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown)
animated:YES];
}
else
{
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
So you don't want to display a WEPopoverfrom an UIBarButtonItem but from an UIButton, right?!
Just use
- (void)presentPopoverFromRect:(CGRect)rect
inView:(UIView *)view
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated;
instead of
- (void)presentPopoverFromBarButtonItem:(UIBarButtonItem *)item
permittedArrowDirections:(UIPopoverArrowDirection)arrowDirections
animated:(BOOL)animated;
and pass in the rect of your UIButton.
Taking your code as an example:
-(IBAction)_clickbtnAccount:(id)sender
{
if (!self.popoverController) {
UIViewController *contentViewController = [[pageAccount alloc]initWithNibName:#"pageAccount" bundle:nil];
self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease];
self.popoverController.delegate = self;
self.popoverController.passthroughViews = [NSArray arrayWithObject:self.navigationController.navigationBar];
UIButton *senderButton = (UIButton *)sender;
[self.popoverController presentPopoverFromRect:[senderButton frame]
inView:self.view
permittedArrowDirections:(UIPopoverArrowDirectionUp|UIPopoverArrowDirectionDown)
animated:YES];
[contentViewController release];
} else {
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
}
WEPopover is basically used to show multiple options inside a popover with much control using UITableview.
This is how i use WEPopOver to show popup on a button click
-(void)showPopOver:(id)sender{
if (self.popoverController) {
[self.popoverController dismissPopoverAnimated:YES];
self.popoverController = nil;
}
PopOverTable *contentViewController = [[PopOverTable alloc] initWithStyle:UITableViewStylePlain];
contentViewController.delegatePopoverItemSelectedDelegate=self;
rectForPopover = [self.view convertRect:btn.bounds fromView:btn];
self.popoverController = [[[popoverClass alloc] initWithContentViewController:contentViewController] autorelease];
if ([self.popoverController respondsToSelector:#selector(setContainerViewProperties:)]) {
[self.popoverController setContainerViewProperties:[self improvedContainerViewProperties]];
}
self.popoverController.delegate = self;
[self.popoverController presentPopoverFromRect:rectForPopover
inView: self.view
permittedArrowDirections:(UIPopoverArrowDirectionAny)
animated:YES];
}
With the accepted solution I get rotation issues for the WEPopover under iOS 7: Basically it was pointing to a custom button in the UIBarButtonItem on the left of the navigation bar but on rotation the pop up moves to the right side of the screen and stays there on further rotations.

Resources