use of undeclared identifier 'prepareForSegue' - objective-c

I'm having two issues with the below code. First, the userCategorySave Parse cloud function is supposed to run when the submit button is tapped, however there are no signs of it running in my logs.
The other problem is an error I'm getting that states use of undeclared identifier 'prepareForSegue'. I was under the impression that prepareForSegue was a standard part of an iOS app, and didn't need to be declared. Why is this class in particular giving me this error?
#import "CriteriaViewController.h"
#interface CriteriaViewController ()
#property (weak, nonatomic) IBOutlet UISegmentedControl *itemConditionSegment;
#property (weak, nonatomic) IBOutlet UISegmentedControl *itemLocationSegment;
#property (weak, nonatomic) IBOutlet UIButton *submitButton;
#end
#implementation CriteriaViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)itemConditionSegment:(id)sender{
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0) {
self.itemCondition = #"new";
}
else{
self.itemCondition = #"any";
}
}
- (IBAction)itemLocationSegment:(id)sender {
UISegmentedControl *segmentedControl = (UISegmentedControl *) sender;
NSInteger selectedSegment = segmentedControl.selectedSegmentIndex;
if (selectedSegment == 0) {
self.itemLocation = #"US";
}
else if (selectedSegment == 1) {
self.itemLocation = #"WorldWide";
}
}
//add all the info to users respective new category object
- (IBAction)submitButton:(id)sender
{
if (self.minPrice.text.length > 0 && self.maxPrice.text.length > 0)
{
[PFCloud callFunctionInBackground:#"userCategorySave"
withParameters:#{#"categoryId": self.chosenCategory,
#"minPrice": self.minPrice.text,
#"maxPrice": self.maxPrice.text,
#"itemCondition": self.itemCondition,
#"itemLocation": self.itemLocation,}
block:^(NSString *result, NSError *error) {
if (!error) {
NSLog(#"Criteria successfuly saved.");
[self performSegueWithIdentifier:#"ShowMatchCenterSegue" sender:self];
}
}];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
#end

You did not close the } correctly.so add one brase in the above method prepare for seque.

Related

How do you add multiple in-app purchases to an iPhone application?

okay, So i basically followed the tutorial in this link
http://www.techotopia.com/index.php/An_iOS_7_In-App_Purchase_Tutorial
I also tried the tutorial in this one: How do you add an in-app purchase to an iOS application? but it did not work for me. I would really appreciate it if someone helps me out. I'm going to use the first link because that's the tutorial I used. It works fine for one in app purchase, BUT, how do i do it for more in app purchases? for more levels? The tutorial only teaches one, and so I try and follow the same tutorial but by changing the names, and it STILL doesn't work. The closest I got was it does purchase it, but it does not enable the button even though I connected the Outlets. Need help, the coding is provided below:
ViewController.h
enter code here#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
#import "PurchaseViewController.h"
#import "SecondPurchaseViewController.h"
#interface MAGViewController : UIViewController
- (IBAction)purchaseItem:(id)sender;
#property (strong, nonatomic) IBOutlet UIButton *level2Button;
#property (strong, nonatomic) PurchaseViewController *purchaseController;
- (IBAction)SecondpurchaseItem:(id)sender;
#property (strong, nonatomic) IBOutlet UIButton *level3Button;
#property (strong, nonatomic) SecondPurchaseViewController *SecondpurchaseController;
-(void)enableLevel2;
-(void)enableLevel3;
#end
And the .m file is: (i did put the #import "PurchaseViewController.h" so it's not that and i also put the Second one as well.
- (IBAction)purchaseItem:(id)sender {
_purchaseController.productID =
#"com.example.IAP.courseone";
[self.navigationController
pushViewController:_purchaseController animated:YES];
[_purchaseController getProductInfo: self];
}
- (IBAction)SecondpurchaseItem:(id)sender {
_SecondpurchaseController.SecondproductID =
#"com.example.IAP.coursetwo";
[self.navigationController
pushViewController:_SecondpurchaseController animated:YES];
[_SecondpurchaseController getSecondProductInfo: self];
}
-(void)enableLevel2
{
_level2Button.enabled = YES;
}
-(void)enableLevel3
{
_level3Button.enabled = YES;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
_purchaseController = [[PurchaseViewController alloc]init];
[[SKPaymentQueue defaultQueue]
addTransactionObserver:_purchaseController];
//
_SecondpurchaseController = [[SecondPurchaseViewController alloc]init];
[[SKPaymentQueue defaultQueue]
addTransactionObserver:_SecondpurchaseController];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
PurchaseViewController.h is
#import <UIKit/UIKit.h>
#import <StoreKit/StoreKit.h>
#interface PurchaseViewController : UIViewController <SKPaymentTransactionObserver, SKProductsRequestDelegate>
#property (strong, nonatomic) IBOutlet UILabel *productTitle;
#property (strong, nonatomic) IBOutlet UITextView *productDescription;
#property (strong, nonatomic) IBOutlet UIButton *buyButton;
- (IBAction)buyProduct:(id)sender;
#property (strong, nonatomic) SKProduct *product;
#property (strong, nonatomic) NSString *productID;
- (void)getProductInfo:(UIViewController *)viewController;
#end
PurchaseViewController.m is:
#interface PurchaseViewController ()
#property (strong, nonatomic) MAGViewController *homeViewController;
#end
#implementation PurchaseViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
//// -->
-(void)getProductInfo: (MAGViewController *) viewController
{
_homeViewController = viewController;
if ([SKPaymentQueue canMakePayments])
{
SKProductsRequest *request = [[SKProductsRequest alloc]
initWithProductIdentifiers:
[NSSet setWithObject:self.productID]];
request.delegate = self;
[request start];
}
else
_productDescription.text =
#"Please enable In App Purchase in Settings";
}
//
//
#pragma mark -
#pragma mark SKProductsRequestDelegate
-(void)productsRequest:(SKProductsRequest *)request didReceiveResponse:(SKProductsResponse *)response
{
NSArray *products = response.products;
if (products.count != 0)
{
_product = products[0];
_buyButton.enabled = YES;
_productTitle.text = _product.localizedTitle;
_productDescription.text = _product.localizedDescription;
} else {
_productTitle.text = #"Product not found";
}
products = response.invalidProductIdentifiers;
for (SKProduct *product in products)
{
NSLog(#"Product not found: %#", product);
}
}
////
- (IBAction)buyProduct:(id)sender {
SKPayment *payment = [SKPayment paymentWithProduct:_product];
[[SKPaymentQueue defaultQueue] addPayment:payment];
}
////
#pragma mark -
#pragma mark SKPaymentTransactionObserver
-(void)paymentQueue:(SKPaymentQueue *)queue updatedTransactions:(NSArray *)transactions
{
for (SKPaymentTransaction *transaction in transactions)
{
switch (transaction.transactionState) {
case SKPaymentTransactionStatePurchased:
[self unlockFeature];
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
case SKPaymentTransactionStateRestored:
NSLog(#"Transaction state -> Restored");
//add the same code as you did from SKPaymentTransactionStatePurchased here
[self unlockFeature];
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
case SKPaymentTransactionStateFailed:
NSLog(#"Transaction Failed");
[[SKPaymentQueue defaultQueue]
finishTransaction:transaction];
break;
default:
break;
}
}
}
////
-(void)unlockFeature
{
_buyButton.enabled = NO;
[_buyButton setTitle:#"Purchased"
forState:UIControlStateDisabled];
[_homeViewController enableLevel2];
}
- (IBAction) restore{
//this is called when the user restores purchases, you should hook this up to a button
[[SKPaymentQueue defaultQueue] restoreCompletedTransactions];
}
- (void) paymentQueueRestoreCompletedTransactionsFinished:(SKPaymentQueue *)queue
{
NSLog(#"received restored transactions: %lul", queue.transactions.count);
for (SKPaymentTransaction *transaction in queue.transactions)
{
if(SKPaymentTransactionStateRestored){
NSLog(#"Transaction state -> Restored");
//called when the user successfully restores a purchase
[self unlockFeature];
[[SKPaymentQueue defaultQueue] finishTransaction:transaction];
break;
}
}
}
//// <--
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
_buyButton.enabled = NO;
}
How do i make it work for more than one IAP purchase? The ray wenderlick tutorial shows in a table view, but i don't want a table view, i want custom buttons with a custom background of my own, so this tutorial is great, but i don't know how to make it work with more than one iap. thank you in advance if u know the answer, please comment. i've been struggling over 2 days already.
In .h file add this method.
+ (RageIAPHelper *)sharedInstance;
in implementaion file .m
+ (RageIAPHelper *)sharedInstance {
static dispatch_once_t once;
static RageIAPHelper * sharedInstance;
dispatch_once(&once, ^{
NSSet * productIdentifiers = [NSSet setWithObjects:
#"com.razeware.inapprage.drummerrage",
#"com.razeware.inapprage.itunesconnectrage",
#"com.razeware.inapprage.nightlyrage",
#"com.razeware.inapprage.studylikeaboss",
#"com.razeware.inapprage.updogsadness",
nil];
sharedInstance = [[self alloc] initWithProductIdentifiers:productIdentifiers];
});
return sharedInstance;
}
by this we can get more productIdentifiers.
follow this tutorial Click Here
Hope this will help you.

presentViewController with storyboards shows black view iOS 7.1 xcode 5.1

i did search SO and the web didn't find amy answer .
i created in storyboard main UITableViewController called A which have simple button.
and another ViewController Called B which have webView and close button.
that is not connected in any form to the main UITableViewController A
now i like to open the B viewController form A and then close the B ViewController with its own close button .
but what ever i try the B view controller is black and blank .
ViewController B ( the popup view )
#import "TAFBLoginDialogViewController.h"
#interface TAFBLoginDialogViewController ()
#end
#implementation TAFBLoginDialogViewController
-(id)initWithAppId:(NSString *)apiKey
requestPremision:(NSString *)requestPremision
delegate:(id<TAFBLoginDialogViewdelegate>)delegate
storyBoardName:(NSString*) storyBoardName
{
//self = [super initWithNibName:#"FBLoginDialog" bundle:nil];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"FBLoginDialog"];
if (self) {
// Custom initialization
self.apiKey = apiKey;
self.requestPremision = requestPremision;
self.delegate = delegate;
}
return self;
}
- (IBAction)closeTap:(id)sender
{
[self.delegate closeTaped];
}
-(void)login
{
}
-(void)logout
{
}
-(void)checkForAccessToken:(NSString*)urlString
{
}
-(void)checkLoginRequired:(NSString*)urlString
{
[self.delegate displayRequired];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
UITableViewController A ( the main view controller from which i try to open B)
#import "TAFMETableViewController.h"
#interface TAFMETableViewController ()
{
}
#end
#implementation TAFMETableViewController
- (id)initWithStyle:(UITableViewStyle)style
{
self = [super initWithStyle:style];
if (self) {
// Custom initialization
}
return self;
}
-(void) awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
#warning Potentially incomplete method implementation.
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
return cell;
}
- (IBAction)handleOpenFBDialog:(id)sender {
UIStoryboard * storyboard = self.storyboard;
NSString * storyboardName = [storyboard valueForKey:#"name"];
self.appId = #"11111";
self.permissions =#"public_stream";
if(_loginDialogView ==nil)
{
self.LoginDialogViewController = [[TAFBLoginDialogViewController alloc] initWithAppId:_appId
requestPremision:_permissions
delegate:self storyBoardName:storyboardName];
self.loginDialogView = _LoginDialogViewController.view;
}
[self.LoginDialogViewController checkLoginRequired:#"tst"];
NSLog(#"Click!");
}
-(void)accessTokenFound:(NSString*)accessToken
{
NSLog(#"accessTokenFound Click!");
}
-(void)displayRequired
{
NSLog(#"displayRequired Click!");
[self presentViewController:_LoginDialogViewController animated:YES completion:nil];
}
-(void)closeTaped
{
NSLog(#"closeTaped Click!");
[self dismissViewControllerAnimated:NO completion:nil];
}
#end
header file :
#protocol TAFBLoginDialogViewdelegate
-(void)accessTokenFound:(NSString*)accessToken;
-(void)displayRequired;
-(void)closeTaped;
#end
#interface TAFBLoginDialogViewController : UIViewController<UIWebViewDelegate>
{
//ivars
// UIWebView *_webview;
// NSString* _apiKey;
// NSString* _requestPremision;
// id <TAFBLoginDialogViewdelegate> _delegate;
}
#property (retain) IBOutlet UIWebView *webView;
#property (copy) NSString *apiKey;
#property (copy) NSString *requestPremision;
#property (assign) id<TAFBLoginDialogViewdelegate> delegate;
-(id)initWithAppId:(NSString*)apiKey
requestPremision:(NSString*)requestPremision
delegate:(id<TAFBLoginDialogViewdelegate>)delegate
storyBoardName:(NSString*) storyBoardName;
- (IBAction)closeTap:(id)sender;
-(void)login;
-(void)logout;
-(void)checkForAccessToken:(NSString*)urlString;
-(void)checkLoginRequired:(NSString*)urlString;
#end
I have button in the TableView A that trigger :
- (IBAction)handleOpenFBDialog:(id)sender
then this function init the ViewController B
and the invoking :
[self.LoginDialogViewController checkLoginRequired:#"tst"];
which supposed to show the ViewController B
but all it does shows black screen .
in your initWithAppID method you instantiate your view controller from the storyboard and then don't do anything with it. Then you have an if (self) initialization block but you've never initialized self.
It looks like what you want to do is set self equal to the view controller that you instantiated from the storyboard and then set the properties on it.
It might make more sense to make this a class method instead since your allocating a view controller object and inside it's init method your creating another one from the storyboard and ignoring the one you allocated.
+(instancetype) TAFBLoginDialogViewControllerWithAppID:(NSString *)apiKey
requestPremision:(NSString *)requestPremision
delegate:(id<TAFBLoginDialogViewdelegate>)delegate
storyBoardName:(NSString*) storyBoardName
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyBoardName bundle:nil];
TAFBLoginDialogViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"FBLoginDialog"];
// Custom initialization
viewController.apiKey = apiKey;
viewController.requestPremision = requestPremision;
viewController.delegate = delegate;
return viewController;
}

OSX: Why my app needs click on it first to receive keydown event?

I currently developing a small dictionary app under OSX for my own use, I would like to have a feature that when I hit the return key, the focus would go to the nssearchfeild.
So I try to make the app to receive keyDown event using a NSView and NSViewController told by this tutorial.
But every time I start the app, it wouldn't receive the keyDown event. I have to click on the window once, then hit the keyboard, so that it can receive keyDown event.
What did I do wrong? Can anyone help me out with this problem? I have been stuck in this problem for days, and searching throught Google and API wouldn't help much.
Thanks in advance!
Here is my code for AppDelegate.m
#import "AppDelegate.h"
#import "MyDictViewController.h"
#interface AppDelegate()
#property (nonatomic,strong) IBOutlet MyDictViewController *viewController;
#end
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
self.viewController = [[MyDictViewController alloc] initWithNibName:#"MyDictViewController" bundle:nil];
[self.window.contentView addSubview:self.viewController.view];
self.viewController.view.frame = ((NSView*)self.window.contentView).bounds;
[self.window makeKeyAndOrderFront:nil];
}
#end
And My ViewController.m
#import "MyDictViewController.h"
#import "FileHelper.h"
#import <Carbon/Carbon.h>
#interface MyDictViewController ()
#property (weak) IBOutlet NSTableView *wordsFilteredTable;
#end
#implementation MyDictViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
self.IMAGE_FILE = [NSImage imageNamed:#"Document.png"];
self.wordlist = [FileHelper readLines];
self.filterWordlist = [[NSMutableArray alloc] init];
}
return self;
}
- (void)loadView
{
[super loadView];
[self.view becomeFirstResponder];
}
-(void)keyDown:(NSEvent*)theEvent
{
NSLog(#"Caught key event");
}
-(void)keyUp:(NSEvent *)theEvent
{
unsigned short keycode = [theEvent keyCode];
switch (keycode)
{
case kVK_Return:
[self.searchField becomeFirstResponder];
default:
break;
}
}
-(void)mouseDown:(NSEvent*)theEvent
{
NSLog(#"Caught mouse event");
}
- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
self.wordsFilteredTable.rowHeight = 37;
NSTableCellView *cellView = [tableView makeViewWithIdentifier:tableColumn.identifier owner:self];
if( [tableColumn.identifier isEqualToString:#"WordColumn"] )
{
NSString *word = [self.filterWordlist objectAtIndex:row];
cellView.textField.stringValue = word;
cellView.imageView.image = self.IMAGE_FILE;
return cellView;
}
return cellView;
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)tableView {
return [self.filterWordlist count];
}
- (void)controlTextDidChange:(NSNotification *)obj
{
NSTextView* textView = [[obj userInfo] objectForKey:#"NSFieldEditor"];
self.currentWord = [textView string];
[self.filterWordlist removeAllObjects];
for(NSString* word in self.wordlist) {
if ([word hasPrefix:self.currentWord]) {
[self.filterWordlist addObject:word];
}
}
[self.wordsFilteredTable reloadData];
}
#end
And my AppView.m
#import "AppView.h"
#implementation AppView
- (void)setViewController:(NSViewController *)newController
{
if (viewController)
{
[super setNextResponder:[viewController nextResponder]];
[viewController setNextResponder:nil];
}
viewController = newController;
if (newController)
{
[super setNextResponder: viewController];
[viewController setNextResponder:[self nextResponder]];
}
}
- (void)setNextResponder:(NSResponder *)newNextResponder
{
if (viewController)
{
[viewController setNextResponder:newNextResponder];
return;
}
[super setNextResponder:newNextResponder];
}
#end

Objective c - Getting text from a UITextfield after keyboard is dismissed

Another NEWBIE QUESTION:
I'm feeling pretty dumb right now. I haven't worked with UI text input for a very long time. So this is what I have so far. I have a view with a subview. I've wired up my IBOutlets for the UITextFields and I've wired the text fields back to the main view controller file owner for the delegate (not sure about that one). I happily build and run. I enter some text and when I click outside the text field my keyboard goes away. But when I exit my view I should be saving the values in each of the fields. But when I step through the code, my textfields (not the text but the fields themselves) are all nil.
So, obviously I've missed an important step here.
Calling saveUserSettings from the main view when user clicks to close the subview:
- (IBAction)closeButtonPressed:(UIButton *)sender {
[self.userSettingsView saveUserSettings];
[self dismissViewControllerAnimated:YES completion:nil];
}
The psychedelic colors are helping me see them during development. :-)
The selected view is a scrollview but it's not scrolling either. That's a different issue.
Here is the IDUtilityViewController code:
#interface IDUtilityViewController ()
#property (strong, nonatomic) IBOutlet IDAboutView *aboutView;
#property (strong, nonatomic) IBOutlet IDUserSettingsView *userSettingsView;
#property (strong, nonatomic) IBOutlet UIButton *aboutButton;
#property (strong, nonatomic) IBOutlet UIButton *userSettingsButton;
#end
#implementation IDUtilityViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.aboutView = [[IDAboutView alloc] init];
self.userSettingsView = [[IDUserSettingsView alloc] init];
[self userSettingsButtonPressed:self.userSettingsButton];
UITapGestureRecognizer* tapGesture = [[UITapGestureRecognizer alloc]initWithTarget:self action:#selector(hideKeyboard)];
tapGesture.cancelsTouchesInView = NO;
[self.view addGestureRecognizer:tapGesture];
}
- (void)hideKeyboard
{
[self.userSettingsView saveUserSettings];
[self.view endEditing:YES];
}
#pragma mark - IBActions
- (IBAction)closeButtonPressed:(UIButton *)sender {
//[self.userSettingsView saveUserSettings];
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)aboutButtonPressed:(UIButton *)sender {
self.userSettingsView.hidden = YES;
self.aboutView.hidden = NO;
}
- (IBAction)userSettingsButtonPressed:(UIButton *)sender {
self.aboutView.hidden = YES;
self.userSettingsView.hidden = NO;
}
Here is the IDUserSettingsView.h:
#interface IDUserSettingsView : UIView
#property (nonatomic, strong) IBOutlet UITextField *userIDField;
#property (nonatomic, strong) IBOutlet UITextField *passwordField;
#property (nonatomic, strong) IBOutlet UITextField *ipAddressField;
#property (nonatomic, strong) IBOutlet UITextField *portNumberField;
#property (nonatomic, strong) IBOutlet UITextField *doorNameField;
#property (nonatomic, strong) IBOutlet UITextField *badgeNumberField;
- (void)saveUserSettings;
#end
Here is the IDUserSettingsView.m:
#import "IDUserSettingsView.h"
#interface IDUserSettingsView()
#property (nonatomic, strong) NSUserDefaults *standardUserDefaults;
#end
#implementation IDUserSettingsView
#synthesize userIDField = _userIDField;
#synthesize passwordField = _passwordField;
#synthesize ipAddressField = _ipAddressField;
#synthesize portNumberField = _portNumberField;
#synthesize doorNameField = _doorNameField;
#synthesize badgeNumberField = _badgeNumberField;
NSMutableString *emptyString = #"";
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.standardUserDefaults = [NSUserDefaults standardUserDefaults];
}
return self;
}
- (void)saveUserSettings
{
[self.standardUserDefaults setObject:self.userIDField.text forKey:#"isonos_userID"];
[self.standardUserDefaults setObject:self.passwordField.text forKey:#"isonos_password"];
[self.standardUserDefaults setObject:self.ipAddressField.text forKey:#"isonos_ipAddress"];
[self.standardUserDefaults setObject:self.portNumberField.text forKey:#"isonos_portNumber"];
[self.standardUserDefaults setObject:self.doorNameField.text forKey:#"isonos_doorName"];
[self.standardUserDefaults setObject:self.badgeNumberField.text forKey:#"isonos_badgeNumber"];
}
- (UITextField *)userIDField {
return _userIDField;
}
- (void)setUserIDField:(UITextField *)userIDField {
NSString *result = [self.standardUserDefaults stringForKey:#"isonos_userID"];
if (!result) result = emptyString;
_userIDField.text = [emptyString stringByAppendingString:result];
}
- (UITextField *)passwordField {
return _passwordField;
}
- (void)setPasswordField:(UITextField *)passwordField {
NSString *result = [self.standardUserDefaults stringForKey:#"isonos_password"];
if (!result) result = emptyString;
_passwordField.text = [emptyString stringByAppendingString:result];
}
- (UITextField *)ipAddressField {
return _ipAddressField;
}
- (void)setIpAddressField:(UITextField *)ipAddressField {
NSString *result = [self.standardUserDefaults stringForKey:#"isonos_ipAddress"];
if (!result) result = emptyString;
_ipAddressField.text = [emptyString stringByAppendingString:result];
}
- (UITextField *)portNumberField {
return _portNumberField;
}
- (void)setPortNumberField:(UITextField *)portNumberField {
NSString *result = [self.standardUserDefaults stringForKey:#"isonos_portNumber"];
if (!result) result = emptyString;
_portNumberField.text = [emptyString stringByAppendingString:result];
}
- (UITextField *)doorNameField {
return _doorNameField;
}
- (void)setDoorNameField:(UITextField *)doorNameField {
NSString *result = [self.standardUserDefaults stringForKey:#"isonos_doorName"];
if (!result) result = emptyString;
_doorNameField.text = [emptyString stringByAppendingString:result];
}
- (UITextField *)badgeNumberField {
return _badgeNumberField;
}
- (void)setBadgeNumberField:(UITextField *)badgeNumberField {
NSString *result = [self.standardUserDefaults stringForKey:#"isonos_badgeNumber"];
if (!result) result = emptyString;
_badgeNumberField.text = [emptyString stringByAppendingString:result];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
I've even tried just setting the controls straight with no values:
#import "IDUserSettingsView.h"
#interface IDUserSettingsView()
#property (nonatomic, strong) NSUserDefaults *standardUserDefaults;
#end
#implementation IDUserSettingsView
#synthesize userIDField = _userIDField;
#synthesize passwordField = _passwordField;
#synthesize ipAddressField = _ipAddressField;
#synthesize portNumberField = _portNumberField;
#synthesize doorNameField = _doorNameField;
#synthesize badgeNumberField = _badgeNumberField;
NSMutableString *emptyString = #"";
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
self.standardUserDefaults = [NSUserDefaults standardUserDefaults];
}
return self;
}
- (void)saveUserSettings
{
[self.standardUserDefaults setObject:self.userIDField.text forKey:#"isonos_userID"];
[self.standardUserDefaults setObject:self.passwordField.text forKey:#"isonos_password"];
[self.standardUserDefaults setObject:self.ipAddressField.text forKey:#"isonos_ipAddress"];
[self.standardUserDefaults setObject:self.portNumberField.text forKey:#"isonos_portNumber"];
[self.standardUserDefaults setObject:self.doorNameField.text forKey:#"isonos_doorName"];
[self.standardUserDefaults setObject:self.badgeNumberField.text forKey:#"isonos_badgeNumber"];
}
- (UITextField *)userIDField {
return _userIDField;
}
- (void)setUserIDField:(UITextField *)userIDField {
_userIDField = userIDField; // breakpoint here
}
- (UITextField *)passwordField {
return _passwordField;
}
- (void)setPasswordField:(UITextField *)passwordField {
_passwordField = passwordField;
}
- (UITextField *)ipAddressField {
return _ipAddressField;
}
- (void)setIpAddressField:(UITextField *)ipAddressField {
_ipAddressField = ipAddressField;
}
- (UITextField *)portNumberField {
return _portNumberField;
}
- (void)setPortNumberField:(UITextField *)portNumberField {
_portNumberField = portNumberField;
}
- (UITextField *)doorNameField {
return _doorNameField;
}
- (void)setDoorNameField:(UITextField *)doorNameField {
_doorNameField = doorNameField;
}
- (UITextField *)badgeNumberField {
return _badgeNumberField;
}
- (void)setBadgeNumberField:(UITextField *)badgeNumberField {
_badgeNumberField = badgeNumberField;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
}
*/
#end
Here is the View Hierarchy:
If you're calling "saveUserSettings" from your main view (and not your IDUserSettingsView), then it's likely the IDUserSettingsView has dismissed and all the outlet connections are long gone.
Why not copy the changed settings into some structure that gets sent/copied into your main view which you can then call "saveUserSettings" on when the user chooses to update his/her settings?
Or, make certain "saveUserSettings" gets called before your IDUserSettingsView dismisses (and everything in that view gets torn down and/or dealloc'd).
You probably messed up the IBOutlet connections, I bet they're not connected or connected to the wrong objects.
Try defining a setter method and putting a breakpoint on it to try and find where it's being set or perhaps cleared to nil:
- (void)setUserIDField:(UITextField *)field
{
_userIDField = field; // breakpoint here
}
(note: if you have ARC disabled you will need to retain the new value and release the old value).

UISlider in xcode

I was wondering if anyone has any idea how to connect a text field with a slider. I want the slider to list numbers from 1-100 or 1-1000. I've looked places but there is no true answer to what I'm looking for. Your help would be greatly appreciated
ViewController.h
#interface ViewController : UIViewController {
IBOutlet UISlider *mySlider;
IBOutlet UITextField *myTextField;
}
#property (nonatomic, retain) IBOutlet UISlider *mySlider;
#property (nonatomic, retain) IBOutlet UITextField *myTextField;
- (IBAction) sliderValueChanged:(id)sender;
- (IBAction) changeButtonPressed:(id)sender;
ViewController.m
#import "ViewController.h"
#implementation TutorialProjectViewController
#synthesize mySlider, myTextField;
- (void)viewDidLoad {
[super viewDidLoad];
self.myTextField.delegate = self;
}
- (IBAction) sliderValueChanged:(UISlider *)sender {
myTextField.text = [NSString stringWithFormat:#" %.1f", [sender value]];
}
- (IBAction) changeButtonPressed:(id)sender {
NSString *textValue = [myTextField text];
float value = [textValue floatValue];
if (value < 0) value = 0;
if (value > 100) value = 100;
mySlider.value = value;
myTextField.text = [NSString stringWithFormat:#"%.1f", value];
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[myTextField resignFirstResponder];
//[put your textfield name here resignFirstResponder];
return NO;
}
#end
Change Slider Value:
I'm assuming you want to update the text field's text as the slider is changed.
You don't connect the two. You need to listen for changes to the slider's value. In the method that you setup for this, you need to convert the slider's value to the text you want. You then update the text field's text with this new text based on the slider's value.
The hook for the slider:
[someSlider addTarget:self action:#selector(sliderUpdate:) forControlEvents:UIControlEventValueChanged];
The method that listens to the slider:
- (void)sliderUpdate:(UISlider *)slider {
float newVal = slider.value;
NSString *newText = [NSString stringWithFormat:#"%d", (int)newVal];
someTextField.text = newText;
}
This code assumes the slider is referenced in the someSlider ivar and the text field is referenced by the someTextField ivar. It also assumes the slider's minimumValue is 0 and the maximumValue is 100 or 1000 as needed.
#import "FirstViewController.h"
#interface FirstViewController ()
#property (weak, nonatomic) IBOutlet UITextField *myTextBox;
#property (weak, nonatomic) IBOutlet UISlider *mySlider;
#property (weak, nonatomic) IBOutlet UINavigationBar *titleOnTop;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;
- (IBAction)changeGreeting:(id)sender;
- (IBAction)sliderValueChanged:(id)sender;
- (IBAction)changeButtonPressed:(id)sender;
#end
#implementation FirstViewController
#synthesize mySlider, myTextBox;
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextBox.delegate = self;
}
- (IBAction) sliderValueChanged:(UISlider *)sender
{
myTextBox.text = [NSString stringWithFormat:#"%.1f", [sender value]];
}
- (void)sliderUpdate:(UISlider *)slider {
float newVal = slider.value;
NSString *newText = [NSString stringWithFormat:#"%d", (int)newVal];
myTextBox.text = newText;
}
- (IBAction) changeButtonPressed:(id)sender
{
NSString *textValue = [myTextBox text];
float value = [textValue floatValue];
if (value < 0) value = 0;
if (value > 1000) value = 1000;
mySlider.value = value;
myTextBox.text = [NSString stringWithFormat:#"%.1f", value];
if ([myTextBox canResignFirstResponder]) [myTextBox resignFirstResponder];
}
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
if (myTextBox)
{
if ([myTextBox canResignFirstResponder]) [myTextBox resignFirstResponder];
}
[super touchesBegan: touches withEvent: event];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
/*
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
*/
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)changeGreeting:(id)sender {
self.username = self.myTextBox.text;
NSString *nameString = self.username;
if ([nameString length] == 0) {
nameString = #"World";
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[myTextBox resignFirstResponder];
return YES;
}
#end
#import "FirstViewController.h"
#interface FirstViewController ()
#property (weak, nonatomic) IBOutlet UITextField *myTextBox;
#property (weak, nonatomic) IBOutlet UISlider *mySlider;
#property (weak, nonatomic) IBOutlet UINavigationBar *titleOnTop;
#property (weak, nonatomic) IBOutlet UIBarButtonItem *backButton;
- (IBAction)changeGreeting:(id)sender;
- (IBAction)sliderValueChanged:(id)sender;
- (IBAction)changeButtonPressed:(id)sender;
- (IBAction)backgroundTouched:(id)sender;
- (IBAction)textFieldReturn:(id)sender;
#end
#implementation FirstViewController
#synthesize mySlider, myTextBox;
- (IBAction)backgroundTouched:(id)sender;
{
[myTextBox resignFirstResponder];
}
- (IBAction)textFieldReturn:(id)sender;
{
[sender resignFirstResponder];
}
- (void)viewDidLoad
{
[super viewDidLoad];
self.myTextBox.delegate = self;
}
- (IBAction) sliderValueChanged:(UISlider *)sender
{
myTextBox.text = [NSString stringWithFormat:#"%.1f", [sender value]];
}
- (void)sliderUpdate:(UISlider *)slider {
float newVal = slider.value;
NSString *newText = [NSString stringWithFormat:#"%d", (int)newVal];
myTextBox.text = newText;
}
- (void)textFieldDidChange:(UITextField*)sender { slider.value = [sender.text floatValue]; }
- (IBAction) changeButtonPressed:(id)sender
{
NSString *textValue = [myTextBox text];
float value = [textValue floatValue];
if (value < 0) value = 0;
if (value > 1000) value = 1000;
mySlider.value = value;
myTextBox.text = [NSString stringWithFormat:#"%.1f", value];
if ([myTextBox canResignFirstResponder]) [myTextBox resignFirstResponder];
}
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
if (myTextBox)
{
if ([myTextBox canResignFirstResponder]) [myTextBox resignFirstResponder];
}
[super touchesBegan: touches withEvent: event];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
// Custom initialization
}
return self;
}
*/
/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
[self.textField addTarget:self action:#selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)viewDidUnload
{
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (IBAction)changeGreeting:(id)sender {
self.username = self.myTextBox.text;
NSString *nameString = self.username;
if ([nameString length] == 0) {
nameString = #"World";
}
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
[myTextBox resignFirstResponder];
return NO;
}
#end