IOS- Pop-up with storyboard - ios

I'm using storyboard and i want to add a popup to my app.
My popup will contain a slider (which i need to get his value), and also cancel option ('x' at top of the popup).
What is the best way to do that?
ViewController?
Xib file?
View?
Thanks in advance!

I think it's better to create a popup, button & slider in your code. Than you have more control over it. It's easier to access your slider & to dismiss the popover in the same code.
Put this code in the IBAction function when a button is pressed from where you want the popover:
//Create a popovercontroller
UIPopovercontroller *popOverController;
//Create an instance of the class you working in, your viewcontroller
YourViewController *popOverContent = [[YourViewController alloc] init];
//Create the popover view, you can change the size of it
UIView *popOverView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UISLider *slider = [[UISlider alloc] initWithFrame:CGRectMake(0, 0, 200, 200)];
UIButton *close = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 50, 20)]
//Add slider & button to view
[popOverView addSubview:slider];
[popOverView addSubview:button];
//Add view to viewcontroller
[popOverContent addSubview:popOverView];
//Alloc & init the popovercontroller
popOverController = [[UIPopoverController alloc] initWithContentViewController:popOverContent];
//Set content size of popover
popOverController.popoverContentSize = CGSizeMake(200, 200);
//present popup from a button or frame
[popOverController presentPopoverFromRect:self.yourButton.frame inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
Than create an IBAction for the close button and use this line of code to dismiss the popover:
[popOverController dismissPopoverAnimated:YES];
You can find more information about popOverControllers here

Depending on what you want to do with your popup and how do you want to present it you can choose:
ViewController from storyboard if you want to present a popup in modal style and from that popup you want to show other view controllers.
Xib file for a custom UIView, if you are using the popup to display some information and get the user input after that you will dismiss the popup and the popup will be used in several places (different view controllers)
Custom UIView in one storyboard view controller, if you want to display some information and get the user input and the popup will be use only on one place (one ViewController)

Related

UISegmentedControl within UIView does not respond to change event

I have programmatically created a basic app on iOS7 SDK :
Created two UINavigationController
Added a view to each of them
Created a UITabBarController with two items
Added each UINavigationController to its respective UITabBarController
This is working fine, and everything is done programmatically. My project only consists in a simple window app template, without any xib nor storyboard, and everything is done within application didFinishLaunchingWithOptions: of my AppDelegate.m.
I am trying to add a UISegmentedControl (still programmatically) as a subview of the view of the first UINavigationController, as follows :
// First navigation controller
UINavigationController *firstNavController = [[UINavigationController alloc] init];
firstNavController.navigationBar.barTintColor = [UIColor redColor];
firstNavController.title = #"First";
firstNavController.navigationBar.topItem.title = #"First top";
firstNavController.tabBarItem.image = [UIImage imageNamed:#"FirstTab"];
// First view
UIView *firstView = [[UIView alloc] init];
firstView.backgroundColor = [UIColor yellowColor];
// Add a segmented control to this view
UISegmentedControl *segmentedControl = [[UISegmentedControl alloc] initWithItems:
[NSArray arrayWithObjects:#"One", #"Two", nil]];
// Register an event handler
[segmentedControl addTarget:self action:#selector(segmentedControlSelectedIndexDidChange:)
forControlEvents:UIControlEventValueChanged];
// Shape it and select its first item
segmentedControl.frame = CGRectMake(10, 100, 300, 20);
segmentedControl.selectedSegmentIndex = 1;
// add the segmented control to the view
[firstView addSubview:segmentedControl];
// and the view to the first tab's navigation controler
[firstNavController.view addSubview:firstView];
The UISegmentedControl DOES show up, but touch events are not handled.
If I change my code and add the UISegmentedControl directly within the UINavigationController, touch events are actually responding :
// and the segmented control directly within the navigation controller
[firstNavController.view addSubview:segmentedControl];
What am I doing wrong, and what should I do to have it respond to it's touch events ?
You need to set the size of the view.
Confusingly a view will be visible even if it is too small to fit it's content but will only respond to touches over the area of it's offical size.
You should not be adding a subview directly to the navigation controllers view.
'firstView' should be encapsulated into a UIViewController. And create your navigation controller with [[UINavigationController alloc] initWithRootViewController:myFirstViewController];

Solve the viewController sizing mystery! Why do elements (UIImageView and Nav bar) change size when switching viewControllers?

So this is the code for my mapView:
self.mapView = [[MKMapView alloc] init];
self.mapView.frame = CGRectMake(0, 70 , self.view.bounds.size.width,
self.view.bounds.size.height);
When I push to this view controller from one viewcontroller it looks just fine. The navigation bar and map view are all in the correct spot; however, when I tried to create a button that go directly to the map Viewcontroller from ANOTHER viewController everything changed.
The map view has shrunk and the navigation bar is missing now..? Here's the new button from the other ViewController:
UIImage* image4 = [UIImage imageNamed:#"Near_me_carousel.png"];
_mapButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 18, 26, 26)];
[_mapButton setBackgroundImage:image4 forState:UIControlStateNormal];
[_mapButton addTarget:self action:#selector(MapButton)
forControlEvents:UIControlEventTouchUpInside];
[_mapButton setShowsTouchWhenHighlighted:YES];
[self.view addSubview:_mapButton];
Here's the method:
-(void) MapButton {
MapViewController *mapView = [[MapViewController alloc] init];
[self.navigationController presentViewController:mapView animated:YES completion:NULL];
}
I'm so confused as to why this is happening! Any ideas?
I don't know if this is really your problem, but here's a shot:
Inside your button method, you are calling your MapViewController modally. If the viewController itself does not have a UINavigationBar, of course it will not be displayed.
When you have a push transition, through a UINavigationController, a navigationBar is automatically added above of your view.
And here's the tricky part: when presented as a push transition, the 0 y value of the view's frame is the point just below the navigationBar. I. e., the navigationBar does not belong to your view.
And when presented through a modal transition (presentViewController:), the 0 y value is the top/left point of the window, even if you add a NavigationBar yourself.
Ilustrating:
The origin of it will be here if presented as a modal:
And here if presented if a push in a navigation stack:
Conclusion:
So, in your case, the y-value 70 in this code
self.mapView.frame = CGRectMake(0, 70 , self.view.bounds.size.width,
self.view.bounds.size.height);
will be different according to the transition style. In a modal, it will look like it's displaced 44 points to the top (size of the navigation bar)

Resign First Responder UITextView. Temporary UINavigationBar?

I am writing an application where I let the user edit some content in a text view, and I want to add a dismiss button, to let the user save there edits, without hacking around to dismiss the keyboard.
I am familiar with dismissing the keyboard programmatically and everything. The only real problem I have now, is a design problem. I want to let the user click a button just like in the notes app, but I don't have a UINavigationBar.
Is it possible to create a temporary Navigation bar and assign it a "done" button?
I am going to implement this in a custom UIView, so it should be controller independent. What I mean is I don't know if there is a UInavigationBar present in the current controller. So that must be dealt with dealt with before adding the temporary one.
Any help is appreciated.
Something like this would work nicely for what you're describing:
In viewDidLoad:
UIToolbar *inputToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:#selector(doneButtonTapped)];
[inputToolbar setItems:[NSArray arrayWithObject:doneButton]];
self.theTextView.inputAccessoryView = inputToolbar;
In doneButtonTapped
[self.view endEditing:YES];

How to create Navigation bar with back button on it?

I am very new to iOS App Development.
In my app I am trying to navigate from second view to first view using navigation bar back button, but while drag and drop the navigation bar in the view I am just able to get the bar with title.
I am really worried and I am not sure what I am doing wrong? I am using Xcode 4.3. and storyboard to design the view.
What i am getting now
Looking for
Thanks for your help guys
Xcode handles this for you.
Highlight your first controller. Go to the menu bar and click Embed In > Navigation Controller. Now using UIButton or something, control-click and drag from your first view controller's button to your second view controller and make it a Push segue. The back button won't appear, but if you run it, it will be there.
So you should have 3 views in storyboard. The Navigation Controller, your first view controller, and your second view controller.
Here is one way to do it using code.
if you want to get a plainButton in the navigation like the iphone`s backButton (only one view), you must add image, i think there is no other way.Of course , if you have 2 or more viewcontrollers , you will use navigationItem.backBarButtonItem without add image .But the first viewcontroller do not have the backButton.
the code like this:
navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
UINavigationItem *navigationItem = [[[UINavigationItem alloc] initWithTitle:#"Detail"] autorelease];
UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 70, 30)];
[button setImage:[UIImage imageNamed:#"plain.png"] forState:UIControlStateNormal];
[button addTarget:self action:#selector(buttonClicked:)
forControlEvents:UIControlEventTouchUpInside];
UIBarButtonItem *buttonItem = [[UIBarButtonItem alloc]
initWithCustomView:button];
navigationItem.leftBarButtonItem = buttonItem;
[buttonItem release];
[button release];
[navigationBar pushNavigationItem:navigationItem animated:NO];
[self.view addSubview:navigationBar];

How do I add a UISegmentedControl to my UIToolbar using Storyboard?

I'm trying to add a Segmented Control to my UIToolbar below, but when I try to drag it over in Storyboard it replaces my Table View.
Also, when I try to add UIBarButton to my Toolbar it pushes my Prototype Cells down... do I have my views hierarchy wrong?
When I add a UIBarButton item anyway, in the simulator the toolbar is not Black Transparent as I set in my navigation control inspector. Whats up with that?
thanks!
To create Segmented Control element in the toolbar with Interface builder it is possible to use the following hint
1) Create Segmented Control Bar Button Item in Navigation Controller:
2) Drag Bar Button Item up to Navigation Item
3) Then you should get the following structure
4) Now you can select Segmented Control to set its properties
1 - Do it programmatically. You need to assign a frame to your UIToolbar first, then you can add elements in it (the segmentedControl). I suggest though to use UIBarButtonItems, which basically are the same.
To stick it to the bottom, try to do this way:
#define SCREEN_FRAME [[UIScreen mainScreen] applicationFrame]
CGRect frame = CGRectMake(0, [[UIScreen mainScreen] bounds].size.height - 44, [[UIScreen mainScreen] bounds].size.width, 44);
UIToolbar *toolBar = [[UIToolbar alloc]initWithFrame:frame];
toolBar.frame = CGRectMake(0,self.view.frame.size.height-toolBar.frame.size.height,SCREEN_FRAME.size.width,toolBar.frame.size.height);
//Setting up the items
UIBarButtonItem *first = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image.png"] style:UIBarButtonItemStylePlain target:self action:#selector(yourAction:)] autorelease];
UIBarButtonItem *second = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:#"image.png"] style:UIBarButtonItemStylePlain target:self action:#selector(yourAction:)] autorelease];
//Creating an array with the items
NSArray *items = [NSArray arrayWithObjects:first,second, nil];
//Assigning the array to the toolBar
[toolBar setItems: items];
Then you can set its style with
mytoolbar.barStyle = UIBarStyleBlack;
2 - Same as above, framing.
3 - You need to set your navigationController style to Black.
Use self.navigationController.navigationBar.style = UIBarStyleBlack; ,or in IB choose the buttons and from Inspector set Tint or Style to whatever you want.
Check also this question
I presume there's a navigation view controller somewhere in here. They have their own toolbar, but it's hidden and empty. The toolbar you're seeing is just a simulation of a toolbar for the purpose of letting you edit the UI.
So to do this with storyboard, don't add the segmented control to the simulated toolbar, create a new toolbar and add it to the tree of views available in your controller (as a sibling to your Table View) and create an IBOutlet to reference it. Then in your viewDidLoad method assign the items in the toolbar to the toolbar already created by the view controller.
In this example I've created a property on my view controller called (cunningly) toolbar:
#property (nonatomic, retain) IBOutlet UIToolbar toolbar;
In my view controller's implementation I assign the toolbar items manually:
-(void)viewDidAppear:(BOOL)animated
{
[self setToolbarItems:self.toolbar.items animated:NO];
[self.navigationController setToolbarHidden:NO animated:YES];
}
You'll need to hide the toolbar when the view controller is popped, for instance in the calling view controller:
-(void)viewWillAppear:(BOOL)animated
{
[self.navigationController setToolbarHidden:YES animated:YES];
}
Assuming you want to hide it, of course.
I did this by placing the UISegmentedController in a Bar Button Item.
Add a toolbar to the UINavigationController window, then enable the display of this toolbar on a ViewController screen.
Drag a bar button into the toolbar on the ViewController screen, and drag a UISegmentedController into that (resizing the button to the full width of the screen)
To wire it all up, ctrl-drag from the UISegmentedController to the corresponding .h file, drag from "referencing outlet" to the yellow view controller icon (using the same variable name), then drag from "ValueChanged" to the yellow icon (selecting the method on the controller that you want to call on a new selection).
This all seems to work as expected. (If you can't select a segment on the controller to set an initial value, you didn't connect up the referencing outlet as described above. That stumped me for a little while.)

Resources