iPhone UISlider not visible - iphone

I want to add a UISlider to my app programmatically without using the IB.
I am adding a UISlider to my UIViewController using the code below. However I don't see the slider when the view comes up. What am I missing? I am using iPhone SDK 3.1.2.
Appreciate any help.
#synthesize slider;
....
- (void)viewDidLoad {
...
...
slider = [[UISlider alloc] initWithFrame: CGRectMake(0, 480 - 80, 300, 20)];
slider.minimumValue = 0.0;
slider.maximumValue = 100.0;
slider.tag = 0;
slider.value = 50;
slider.continuous = YES;
slider.enabled = YES;
[slider addTarget:selfaction:#selector(handleSlider:)forControlEvents:UIControlEventValueChanged];
self.view addSubview:slider];
In the .h file
...
UISlider *slider;
...
#property (nonatomic, retain) UISlider *slider;
- (void) handleSlider:(id)sender;

Possibilities:
If the slider isn't the last view added, its likely its being hidden by another view.
The self.view is actually smaller than {0,400}.
The sliders hidden attribute is set to true.
You have more than one view controller active at a time.
You should check the debugger for the self.view's subviews and see if the slider is there. If so then it's almost certainly one of the reasons above.

Related

How to add a button on the Google maps in iOS?

I am new to iOS Programming and I have downloaded the google maps sdk for iOS and followed the instruction on their website ( as shown in this link
https://developers.google.com/maps/documentation/ios/start
)
and was able to get the map in my application.
Now I am trying to add a button on the screen at the bottom over Google maps for giving an option to the user to go back to the previous screen.
I just know that UIButton is a subclass of UIView and we can make a button appear on a view by making it the sub view of that class. Previously iOS used to use Google Maps by default by MKMapView and I have seen examples in books an on the Internet showing screen shots of apps where a button or a text box would appear on the map. But now just dragging the button in the interface builder doesn't help with the SDK of google maps.
Here is my code:
ViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UIButton *btn;
#end
ViewController.m
#import "ViewController.h"
#import <MapKit/MapKit.h>
#import <GoogleMaps/GoogleMaps.h>
#import <CoreLocation/CoreLocation.h>
#interface ViewController ()
#end
#implementation ViewController
{
GMSMapView *mapView_;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)loadView
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];
//Latitude and longitude of the current location of the device.
double lati = locationManager.location.coordinate.latitude;
double longi = locationManager.location.coordinate.longitude;
NSLog(#"Latitude = %f", lati);
NSLog(#"Longitude = %f", longi);
CLLocation *myLocation = [[CLLocation alloc] initWithLatitude:lati longitude:longi];
// Create a GMSCameraPosition that tells the map to display the coordinate
GMSCameraPosition *camera = [GMSCameraPosition cameraWithLatitude:lati
longitude:longi
zoom:11.5];
mapView_ = [GMSMapView mapWithFrame:[[UIScreen mainScreen] bounds] camera:camera];
mapView_.myLocationEnabled = YES;
self.view = mapView_;
// Creates a marker in the center of the map.
GMSMarker *marker = [[GMSMarker alloc] init];
marker.position = CLLocationCoordinate2DMake(lati, longi);
marker.title = #"It's Me";
marker.snippet = #"My Location";
marker.map = mapView_;
[mapView_ addSubview:_btn];
[mapView_ bringSubviewToFront:_btn];
}
#end
You can see that in the last 2 lines I have made the button the subview of mapview and tried to bring it front. But this didn't help. Please let me know what is it that I am missing or if there is another way to do this by using some other function.
Please also do check the screenshot of the storyboard which I have created so that you can understand better what I am trying to do here.
Thanks.
GMSMapView is subclass of UIView so you can add subviews as to any other view
Try this code
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(mapView_.bounds.size.width - 110, mapView_.bounds.size.height - 30, 100, 20);
button.autoresizingMask = UIViewAutoresizingFlexibleLeftMargin | UIViewAutoresizingFlexibleTopMargin;
[button setTitle:#"Button" forState:UIControlStateNormal];
[mapView_ addSubview:button];
It adds 100x20 button as the subview of the GMSMapView, positioned to the bottom right corner. I have tested it and the button can be touched as in any other view
Edit:
Also move all your code from -loadView to -viewDidLoad. -loadView method is never called when using IB to create UI. Docs to -loadView says:
If you use Interface Builder to create your views and initialize the view controller, you must not override this method.
Edit 2:
I believe when you create view hierarchy using Interface Builder, you CAN NOT reset self.view property like you are doing.
Do this in your -viewDidLoad
[self.view addSubview: mapView_];
instead of
self.view = mapView_;
if you are passing GMSMapView to the self.view property, the map is only view which is in the controller from this point. Thats, I believe, the reason why u can't see your IB-created button.
Make your view normally with InterfaceBuilder and to put mapView at 0 index subview :
mapView_ = [GMSMapView mapWithFrame:self.view.bounds camera:camera];
mapView_.myLocationEnabled = YES;
[self.view insertSubview:mapView_ atIndex:0];
Thanks to Raul Clauss from this thread
And you can custom the size of mapView, change :
mapView_ = [GMSMapView mapWithFrame:_mapHolder.bounds camera:camera];
mapView_.myLocationEnabled = YES;
[_mapHolder insertSubview:mapView_ atIndex:0];
Where mapHolder, is a UIView made inside the IB.
I faced the same issue and the fix is very easy, just override this UIViewController method:
-(void) viewWillAppear:(BOOL)animated and put your UIButton creation logic inside that method.
Example:
-(void) viewWillAppear:(BOOL)animated
{
locationButton = [UIButton buttonWithType:UIButtonTypeCustom];
locationButton.frame = CGRectMake(0, 30, self.view.frame.size.width/6, self.view.frame.size.height/6);
[locationButton setImage:[UIImage imageNamed:#"location_enabled.png"] forState:UIControlStateNormal];
[self.view addSubview:locationButton];
}
This problem occur because you try to add the map view with the code, earlier you add the button with the help of interface builder. When you add the mapView it would add at the top of the view that's why your button is not shown.
You have two options for doing this.
First approach
Drag and drop the UIButton from the interface
builder and set the constraints accordingly
Then create the outlet of that button in the corresponding viewController
#IBOutlet weak var bottonToAdd: UIButton!
After adding the map view to the view paste the blow code to bring the button at the top
mapViewForScreen.addSubview(self.bottonToAdd)
mapViewForScreen.bringSubviewToFront(self.bottonToAdd)
After the above three step you can find out that you hav the button over mapview
Second approach
If you don't want to use the interface builder you can use the below code for the same.
var bottonToAdd:UIButton = UIButton()
bottonToAdd.setTitle("Button To Add", for: .normal)
bottonToAdd.sizeToFit()
bottonToAdd.center = mapViewForScreen.center
bottonToAdd.tintColor = UIColor.blue
mapViewForScreen.addSubview(bottonToAdd)
mapViewForScreen.bringSubviewToFront(bottonToAdd)
let mapButton:UIButton = UIButton(frame: CGRect(x:self.view.bounds.width-50, y: 20, width: 40, height: 40)) //Swfit 5
mapButton.backgroundColor = UIColor.clear
mapButton.setImage(UIImage(named: "list"), for: UIControl.State.normal)
mapButton.setImage(UIImage(named: "map"), for: UIControl.State.selected)
mapButton.addTarget(self, action:#selector(self.mapListClicked), for: .touchUpInside)
self.view.addSubview(mapButton)

Adding a view dynamically with in the same view

i have a view which contains some text fields and picker views and one button,when clicked on the button all these textfields and picker view should appear again in the same view.Now how should i take the initial view and how to load the same view with the previous view when clicked on the button.
Thanks in advance..
Setting the Frames an Adding subview (text fields and picker views) on the button click , you can appear those subviews to the main view.
Or else you can initially put them all in the main view and initially hide them. And on the button click set them Hidden : NO.
Here is the approach:
On your View controller : Add a button and a UIScrollView.
Create an independent view say MyView containing all your TextFields and pickers. (you can create this via xib or via code)
In ViewDidLoad method of your controller add this MyView on UIScrollView.
On onButtonPressed similarly add MyView on UIScrollView.
If I got your problem then this code might help you. I am adding only text field on button click event. So I think it might give you idea for another views and you can do the same with another views. I am giving you .m file, hope you can write .h file. Here is my .m file looks like -
#import "ViewController.h"
#interface ViewController ()
#end
#implementation ViewController
#synthesize btn = _btn; // IBOutlet of UIButton
#synthesize txtFieldOne = _txtFieldOne; // IBOutlet of UITextField
#synthesize txtFieldTwo = _txtFieldTwo; // IBOutlet of UITextField
- (void)viewDidLoad
{
newYCoordinateForNewTxtFld = 40.0; // newYCoordinateForNewTxtFld is of type CGFloat
frame = self.txtFieldTwo.frame; // frame is of type CGRect
[super viewDidLoad];
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return true;
}
// TextField Delegate Method you might required in your project
- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
- (IBAction)btnClicked:(id)sender
{
// I am giving u a simple idea, so written simple code. You can make code more usable with ur view by adding various functions, loops, etc ,etc.
int tagValue = 0;
UITextField *newTextField = [[UITextField alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y+newYCoordinateForNewTxtFld, frame.size.width, frame.size.height)];
newTextField.delegate = self;
newTextField.tag = tagValue++;
frame = newTextField.frame;
newTextField.backgroundColor = [UIColor whiteColor];
[newTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:newTextField];
newTextField = nil;
newTextField = [[UITextField alloc]initWithFrame:CGRectMake(frame.origin.x, frame.origin.y+newYCoordinateForNewTxtFld, frame.size.width, frame.size.height)];
newTextField.tag = tagValue++;
newTextField.delegate = self;
frame = newTextField.frame;
newTextField.backgroundColor = [UIColor whiteColor];
[newTextField setBorderStyle:UITextBorderStyleRoundedRect];
[self.view addSubview:newTextField];
}
#end

how to load uiview from nib

I am trying to change views when I swipe. I have the swipe working find as I have tested this by changing the background color.
Since then however I have added a new view.nib and set the class to be the same as the current view.
Inside this classes .h file I have done this
#interface MasterViewController : UIViewController <UIGestureRecognizerDelegate>{
UIView *myView;
UIView *secondView;
}
#property (strong, nonatomic) IBOutlet UIView *myView; // attached to Viewcontroller nib
#property (strong, nonatomic) IBOutlet UIView *secondView; // attached to the secondView
- (void)swipedScreen;
MyView is the main view that appears first, secondView is the view of the nib that I have created and changed its class to relate to this view.
From there I have done this in the .m file
- (void) setupSwipeGestureRecognizer {
UISwipeGestureRecognizer *swipeGesture = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:#selector(swipedScreen)];
swipeGesture.direction = (UISwipeGestureRecognizerDirectionRight|UISwipeGestureRecognizerDirectionLeft);
[myView addGestureRecognizer:swipeGesture];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
self.title = #"Prototype";
[self setupSwipeGestureRecognizer];
}
- (void)swipedScreen
{
// Not sure what to do here.
[[NSBundle mainBundle] loadNibNamed:#"SecondView" owner:self options:nil];
}
I just dont know what to do in swipedScreen method to get the secondView to appear.. I would like to animate this view to slid in from the right.. but at the moment that comes secondary to actually just getting the view to appear... not sure what I am doing wrong here but obviously its something quite fundamental.
any help would be greatly appreciated.
As I commented:
- (void)swipedScreen
{
[UIView animateWithDuration:3.0f delay:0 options:UIViewAnimationOptionCurveLinear
animations:^{
secondView.frame = CGRectMake(180, secondView.frame.origin.y, secondView.frame.size.width, secondView.frame.size.height);
}
completion:^(BOOL finished){
myView.hidden = YES;
}];
}
Actually, you'll have to change the X, Y, Width and height values as you want to animate, and when it's completed, I set the main view - myView - hidden.

Unable to set (make it smaller) Core Plot Pie Chart size

Am a newbie to Core Plot & to iOS/Cocoa. I have a view from which am calling another view (called MyGraphView) which has the CorePlot graph.
The MainView.h
#interface MainView : UIViewController {
...
}
MainView.m
....
....
#implementation MyView
....
MyGraphView *myGraphView;
....
....
// This gets called when a button's pressed. Once the button's pressed,
// I want to display the view which has the graph i.e. pie chart
-(IBAction) toDateChosen:(id)sender {
....
[self presentModalViewController:myGraphView animated:YES];
....
}
....
....
- (void)viewDidLoad {
...
myGraphView = [[EmotionsHistoryView alloc] initWithNibName:nil bundle:nil];
....
}
And in the MyGraphView.h
#import <UIKit/UIKit.h>
#import "CorePlot-CocoaTouch.h"
#interface MyGraphView : UIViewController <CPTPlotDataSource> {
....
....
CPTXYGraph *graph;
CPTPieChart *pieChart;
}
#property (nonatomic, retain) CPTXYGraph *graph;
-(void) initializeGraph;
-(void) initializePieChart;
....
....
In MyGraphView.m
....
#implementation MyGraphView
....
-(void) initializeGraph {
graph = [[CPTXYGraph alloc] initWithFrame:CGRectZero];
CPTGraphHostingView *hostingView = (CPTGraphHostingView *)self.view;
hostingView.hostedGraph = graph;
//hostingView.bounds = CGRectMake(5, 5, 70, 70);
[self initializePieChart];
}
....
....
-(void) initializePieChart {
pieChart = [[CPTPieChart alloc] initWithFrame:CGRectMake(25, 25, 50, 20)];
pieChart.dataSource = self;
pieChart.pieRadius = 100.0;
pieChart.opaque = FALSE;
pieChart.pieRadius = 60;
pieChart.shadowOffset = CGSizeMake(-1, 1);
pieChart.identifier = #"Identifier1";
pieChart.startAngle = M_PI_4;
pieChart.sliceDirection = CPTPieDirectionCounterClockwise;
pieChart.labelOffset = -0.6;
[graph addPlot:pieChart];
}
....
....
When this graph view is shown, I also want to show a button below the graph which will take the user to another screen. But I tried various ways to do it, in vain. I tried reducing the graph size using bounds for the frame of the view, in specifying it in 'init' for both the graph and pie chart, in vain. The graph seems to inevitably occupy the entire view. I also tried adding a button in the MyGraphView.xib file (in Interface Builder) but the button doesn't show up when I run the App in iOS Simulator as the graph occupies the entire view/screen.
Any pointers please?
I use iOS 5.0 and Xcode 4.2, along with the iOS Simulator to run the App.
Thank you
Don't add the button as a subview of the hosting view. The flip transform used by Core Plot will flip the button, too. Instead, make both the hosting view and button subviews of another view. They can be positioned in the superview like any other views.

UIScrollView crashes app when touched

I am running into a very annoying problem: I am creating an UIScrollView that containes an UIView that contains some buttons. The UIView with buttons work fine. But the UIScrollView, no matter what I do with it, when touched, crashes. It doesn't make any difference it's empty or not. It keeps crashing.
I am very lost and don't know what else to try.
Thanks very much.
In the viewController.h
#interface tagstestViewController : UIViewController <UIScrollViewDelegate> {
UIScrollView *scrollViewContainer;
}
#property (nonatomic, retain) UIScrollView *scrollViewContainer;
In the viewController.m:
UIScrollView *scv = [[UIScrollView alloc] initWithFrame:CGRectMake(0,0, 320, 200)];
scv.backgroundColor = [UIColor blackColor];
scv.autoresizingMask = UIViewAutoresizingFlexibleHeight;
scv.bounces = YES;
scv.scrollEnabled = YES;
scv.clipsToBounds = YES;
scv.delegate = self;
[self setScrollViewContainer:scv];
[scv release];
[self.view addSubview:scrollViewContainer];
Are you releasing your viewController right after you add its view into the view hierarachy? Because if you do that, I noticed that even though the application keeps running fine, if you use "scv.delegate = self", it will crash when it tries to deliver the delegated messages to the viewController.