how to align an image in UISlider? - objective-c

I change the swipe of a UISlider button for a picture. the problem is that was out of place, how can I solve this?
link img -> http://i.imgur.com/5L5IsB6.png
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
zoomValue = [[defaults objectForKey:#"music.zoom.value"] floatValue];
self.slider.value = zoomValue;
[self.slider setThumbImage:[UIImage imageNamed:#"btn-tamanho-fonte"] forState:UIControlStateNormal];
}

Subclass UISlider and override thumbRectForBounds:trackRect:value:.

Related

I want to stay on logout page, when application runs again (if user did not hit logout button)

Below is my code till sign in process
#import "ViewController.h"
#import "LogOutViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userid = [defaults objectForKey:#"uid"];
NSString *password = [defaults objectForKey:#"pswrd"];
_lbluserid.text = userid;
_lblpswrd.text = password;
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnsignin:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:_lbluserid.text forKey:#"uid"];
[defaults setObject: _lblpswrd.text forKey:#"pswrd"];
[defaults synchronize];
NSLog(#"Credentials are saved");
LogOutViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"logoutvc"];
controller.getUserid = _lbluserid.text;
[self.navigationController pushViewController:controller animated:YES];
}
#end
I have another view which will appear by signing in, on that view I have Log Out button, Now what I want is if user didn't hit logout button the same page (log out page) will be displayed when I run my app again.
Please help me..
try amend you code to.
#import "ViewController.h"
#import "LogOutViewController.h"
#interface ViewController ()
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSString *userid = [defaults objectForKey:#"uid"];
NSString *password = [defaults objectForKey:#"pswrd"];
_lbluserid.text = userid;
_lblpswrd.text = password;
if([defaults boolForKey:#"logedIn"]){
[self moveToLogoutViewController];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)btnsignin:(id)sender
{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setObject:_lbluserid.text forKey:#"uid"];
[defaults setObject: _lblpswrd.text forKey:#"pswrd"];
[defaults setBool: YES forKey:#"logedIn"];
[defaults synchronize];
NSLog(#"Credentials are saved");
[self moveToLogoutViewController];
}
- (void) moveToLogoutViewController{
LogOutViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"logoutvc"];
controller.getUserid = _lbluserid.text;
[self.navigationController pushViewController:controller animated:YES];
}
#end
You can try like this
First Open AppDelegate class in didFinishLaunchingWithOptions method.
Check user is exist or not by using NSUserDefaults.If user exist you can show LogOutViewController
Use keychain to store user credentials. In your viewCOntroller class, in viewDidLoad check if user credentials exist or not.If exist, then call:
LogOutViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:#"logoutvc"];
controller.getUserid = //get user id from keychain;
[self.navigationController pushViewController:controller animated:YES];

Switch Between 2 UIViewControllers in a NavigationBar Underneath

I have built a UIViewController to contain two other UIViewControllers. What I would like to do using a UISegment in the UINavigationBar is to switch between both views. When the user is finished they click on "Done" and it goes back to the RootViewController. I got this to work except the UIViewControllers take the size of the whole screen so the top portion is covered by the UINavigationBar. They don't adjust to the space under the UINavigationBar. I have been trying different techniques without success.
I have already tried in the ChildUIViewControllers without success
self.edgesForExtendedLayout = UIRectEdgeNone;
Here is my code:
RootViewController Calls the Container
ContainerController *controller = [[ContainerController alloc] init]
[self.navigationController pushViewController:controller animated:YES];
Container Code
- (void)viewDidLoad {
[super viewDidLoad];
UIViewController *vc = [self getCurrentViewController]; // Returns Current ViewController to Display
[self addChildViewController:vc];
vc.view.frame = self.view.bounds;
[self.view addSubview:vc.view];
self.currentViewController = vc;
}
-(UIViewController *)getCurrentViewController{
UIViewController *vc;
switch ([[NSUserDefaults standardUserDefaults] integerForKey:#"currentView"]) {
case kView1:{
vc = (ViewController1 *)[[ViewController1 alloc] init];
}break;
case kView2:{
vc = (ViewController2 *)[[ViewController2 alloc] init];
}break;
}
return vc;
}
- (void)segmentedControlIndexChanged:(id)sender{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[NSNumber numberWithInt:[sender selectedSegmentIndex]] forKey:#"currentView"];
[userDefaults synchronize];
UIViewController *vc = [self getCurrentViewController];
[self addChildViewController:vc];
[self transitionFromViewController:self.currentViewController toViewController:vc duration:0.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
[self.currentViewController.view removeFromSuperview];
vc.view.frame = self.view.bounds;
[self.view addSubview:vc.view];
} completion:^(BOOL finished) {
[vc didMoveToParentViewController:self];
[self.currentViewController removeFromParentViewController];
self.currentViewController = vc;
}];
self.navigationItem.title = vc.title;
}
Anybody know what the problem is? Thanks.
I found a solution. The reason that the tableview was under the navigationbar was due to the translucence of the navigation bar. When I set this:
self.navigationController.navigationBar.translucent = NO;
The views remained under the navigation bar. There was still a black bar above the UITableView I fixed that problem by instead of using a UITableViewController I user a UIViewController and added a tableView. Here is some code to make cycle between the two views in the container uiviewcontroller:
- (void)viewDidLoad {
self.viewController1 = [[ViewController1 alloc] init];
self.viewController2= [[ViewController2 alloc] init];
switch ([[NSUserDefaults standardUserDefaults] integerForKey:#"dataView"]) {
case kDataView:{
self.viewController1.view.hidden=YES;
self.viewController2.view.hidden=NO;
}break;
case kChartView:{
self.viewController1.view.hidden=NO;
self.viewController2.view.hidden=YES;
}break;
}
[self.view addSubview:self.viewController1.view];
[self.view addSubview:self.viewController2.view];
}
- (void)segmentedControlIndexChanged:(id)sender{
if ([[NSUserDefaults standardUserDefaults] integerForKey:#"dataView"]!=[sender selectedSegmentIndex]){
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:[NSNumber numberWithInteger:[sender selectedSegmentIndex]] forKey:#"dataView"];
[userDefaults synchronize];
self.segmentedControl.selectedSegmentIndex=[[NSUserDefaults standardUserDefaults] integerForKey:#"dataView"];
switch (self.segmentedControl.selectedSegmentIndex) {
case kDataView:{
self.viewController1.view.hidden=YES;
self.viewController2.view.hidden=NO;
}break;
case kChartView:{
self.viewController1.view.hidden=NO;
self.viewController2.view.hidden=YES;
}break;
}
}
}

Checkbox go to another view controller objective c

I am trying to create a checkbox button, when the user click on that button it will check in and take him to another view controller. below is my checkbox code, can anyone help how can send the user to the other view controller?
Note I am using a storybored.
The header
IBOutlet UIButton *Checkbox;
BOOL checked;
(IBAction)CheckboxAction:(id)sender;
and here is the code:
- (void)viewDidLoad : (BOOL)animated
{NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
checked = [defaults boolForKey:#"boxIsChecked"];
[self checkTheBox];}
-(void) checkTheBox
{
if (!checked)
{[Checkbox setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];}
else if (checked)
{[Checkbox setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];}
}
- (IBAction)CheckboxAction:(id)sender
{NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (!checked)
{[Checkbox setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
checked = YES;
[defaults setBool:checked forKey:#"boxIsChecked"];}
else if (checked)
{[Checkbox setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
checked = NO;
[defaults setBool:checked forKey:#"boxIsChecked"];}
[defaults synchronize];
}
- (IBAction)CheckboxAction:(id)sender {
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (!checked) {
[Checkbox setImage:[UIImage imageNamed:#"checkBoxMarked.png"] forState:UIControlStateNormal];
checked = YES;
[defaults setBool:checked forKey:#"boxIsChecked"];
}
else if (checked) {
[Checkbox setImage:[UIImage imageNamed:#"checkBox.png"] forState:UIControlStateNormal];
checked = NO;
[defaults setBool:checked forKey:#"boxIsChecked"];
}
[defaults synchronize];
// ADD THIS
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
YourViewController *yourVC = (YourViewController *)[storyboard instantiateViewControllerWithIdentifier:#"YourViewControllerIdentifier"];
[self.navigationController pushViewController:yourVC animated:YES];
}

viewDidLoad not running when expected

I have the following code in my viewDidLoad to change an image:
- (void)viewDidLoad
{
[super viewDidLoad];
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if ([defaults objectForKey:#"colour"] == nil) {
[defaults setObject:#"Red" forKey:#"colour"];
[defaults setInteger:0 forKey:#"colourIndex"];
[defaults synchronize];
}
[background setImage:[UIImage imageNamed:[defaults objectForKey:#"colour"]]];
NSLog(#"%#", [defaults objectForKey:#"colour"]);
// Logs "Blue"
}
When I open my flipside view in the iPhone Simulator and then go back to my original view, the result of the NSLog doesn't change and the image doesn't change. Why is this happening?
Move this code to viewWillAppear. viewDidLoad is not called every time the view is presented.
This link provides a good explanation.
UIViewController viewDidLoad vs. viewWillAppear: What is the proper division of labor?

Load a .nib at first app launch

I want to show the "intro" nib i made only at first app launch.
I was using the following code in my viewDidLoad but it seems to do nothing (even in ViewWillAppear). I tried to clean, remove the app from simulator and device and build again but nothing happened.
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"]){
IntroViewController *intro = [[IntroViewController alloc] initWithNibName:nil bundle:nil];
intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
[self presentModalViewController:intro animated:YES];
[intro release];
[defaults setObject:[NSDate date] forKey:#"firstRun"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
I also tried to show a little UIAlertView at first launch, and it works! Am i failing to load the nib?
EDIT
I forgot to say it's a tab Bar based app and i've some code in my app delegate to highlight rows at the first three sessions of the app.
Any help appreciated!
Ok, add the following code and it should work:
- (void)Loadview_afterdelay{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"firstRun"]){
IntroViewController *intro = [[IntroViewController alloc] initWithNibName:nil bundle:nil];
intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
[self presentModalViewController:intro animated:YES];
[intro release];
[defaults setObject:[NSDate date] forKey:#"firstRun"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)viewDidLoad {
[self performSelector:#selector(Loadview_afterdelay) withObject:nil afterDelay:0.5];
}
Apparently, in this case, you can't load a second view right after your view is loaded (viewdidload)
Therefor you just set a small delay before loading your second view.
Try the following:
IntroViewController *intro = [[IntroViewController alloc] initWithNibName:#"IntroViewController" bundle:nil];
intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
[self presentModalViewController:intro animated:YES];
[intro release];
I have added your .nib name to initWithNibName
Also, if you're using iOS 5 (and ARC), you should use this:
IntroViewController *intro = [[IntroViewController alloc] initWithNibName:#"IntroViewController" bundle:nil];
intro.modalTransitionStyle = UIModalTransitionStyleCoverVertical ;
[self presentModalViewController:intro animated:YES completion:nil];
Have you tried moving it to viewDidAppear?
It looks to me that you should place that code in your AppDelegate
- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
// Modify the code above to what you need.
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
[application setStatusBarHidden:YES withAnimation:NO];
return YES;
}
It is there that your telling your application what view to start with.
So first time have a firstTimeViewController.
If you are doing it here don't animate the transition in.
Just animate the transition off.
Put a break point and check if you are going into the viewDidLoad method and if so check if your test is appropriate.
I suggest maybe using a BOOL in an NSNumber to check agains.
On top of that you may consider the option to put that in viewDidAppear.
As #Louis already answered the question very well already the only thing is It's just not compatible with the ARC since there are a [ release] What I'll write now isn't new but I'll declare it more for the beginner ones.
First you have to import the TourGuideViewController.h in your RootViewController.h which will be like similarly like this:
#import <UIKit/UIKit.h>
#import "TourGuideViewController.h"
#interface RootViewController : UITabBarController
{
TourGuideViewController *TourGuideViewController;
}
#property (nonatomic, strong) TourGuideViewController *TourGuideViewController;
#end
Then You have to synthesize and implement your function in your RootViewController.m file:
#import "RootViewController.h"
#interface RootViewController ()
#end
#implementation RootViewController
#synthesize TourGuideViewController = _TourGuideViewController;
- (void)viewDidLoad
{
[self performSelector:#selector(LoadTourGuide) withObject:nil afterDelay:0.5];
}
- (void)LoadTourGuide{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
if (![defaults objectForKey:#"FirstRun"]){
TourGuideViewController *TourGuideVC = [self.storyboard instantiateViewControllerWithIdentifier:#"TourGuideViewController"];
TourGuideVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
[self presentViewController:TourGuideVC animated:YES completion:NULL];
[defaults setObject:[NSDate date] forKey:#"FirstRun"];
}
[[NSUserDefaults standardUserDefaults] synchronize];
}
#end