I know it was trivial but I have messed myself up and spent hours.
I have a UIViewController which is composed of CategoryTableview and ProductsTableView.
The ProductsTableView is creating the customized TableViewCell called ProductTableViewCell.
The ProductTableViewCell has title label, detail label, price label and a customized stepper to control the amount of the products which is subclassing from UIViewController.
The stepper, is composed with two buttons and a textfield.
I think the way I'm implementing the program is more complex than it should be.
And my question is how to detect the value changed by the stepper controller in the parent view controller.
The code is:
ProductionViewController.h (parent view controller for listing everything)
#interface ProductionViewController ()<UITableViewDataSource, UITableViewDelegate>
#property (nonatomic,strong) UITableView* categoryTable;
#property (nonatomic,strong) UITableView* productsTable;
#property (nonatomic,strong) CheckOutBar* checkOutBar;
//datasources
#property (nonatomic,strong) NSMutableArray* menuLists;
#property (nonatomic,strong) NSMutableArray* detailLists;
//datefield
#property (nonatomic,strong) NSString* startDate;
#property (nonatomic,strong) NSString* endDate;
the customized cell for products is
#interface ProductCell : UITableViewCell
#property (nonatomic, strong) UIImageView* eqiupmentImage;
#property (nonatomic, strong) UILabel* Title;
#property (nonatomic, strong) UILabel* details;
#property (nonatomic, strong) UILabel* prices;
#property (nonatomic, strong) MCStepperViewController* stepper;
The implementing method of the cell is
#implementation ProductCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
{
_Title = [[UILabel alloc] init];
_eqiupmentImage = [[UIImageView alloc] init];
_details = [[UILabel alloc] init];
_details.font = kFONT(12);
_details.textColor = KColor_Gray;
_prices = [[UILabel alloc] init];
_stepper = [[MCStepperViewController alloc] init];
[self.contentView addSubview:_stepper.view];
[self.contentView addSubview:_Title];
[self.contentView addSubview:_eqiupmentImage];
[self.contentView addSubview:_details];
[self.contentView addSubview:_prices];
}
return self;
}
-(void)layoutSubviews
{
[super layoutSubviews];
[_eqiupmentImage setFrame:CGRectMake(kLeftMargin, kLeftMargin, 80 ,80)];
[_Title setFrame:CGRectMake(10+CGRectGetMaxX(_eqiupmentImage.frame), kLeftMargin/2, 60, 20)];
[_details setFrame:CGRectOffset(_Title.frame, 0, 20)];
[_prices setFrame:CGRectMake(10+CGRectGetMaxX(_eqiupmentImage.frame), CGRectGetMaxY(_eqiupmentImage.frame)-20, 40, 20)];
[_stepper.view setFrame:CGRectOffset(_prices.frame, 40, 0)];
}
The customized stepperview is
MCStepperViewController.h (for controlling the amount of products)
#interface MCStepperViewController : UIViewController
#property (nonatomic, strong) UITextField *amountText;
#property (nonatomic, strong) UIButton * plusButton;
#property (nonatomic, strong) UIButton * minusButton;
#property (nonatomic, assign) int max;
#property (nonatomic, assign) int min;
#property (nonatomic, assign) int amount;
Thank you
One way could be implementing a protocol in your cells with a method like - (void)productCell:(ProductCell *)cell didChangeStepperValue:(NSInteger)stepperValue. Then you make your view controller a delegate of each cell and call this method every time the stepper changes it's value. To separate cells from each other you could assign their tag value to your model's corresponding index.
Another way is to use KVO, but you should be careful with subscribing/unsubscribing. And using blocks, of course, is a viable option.
Related
I have been following online tutorials over at raywenderlich.com, and came upon a little road block. In a tutorial (Quote Quiz), I seem to have done something wrong because the strings, extracted from the Quote.plist, is simply not displaying on the question and answers labels.
Here is my view controller.h:
#import <UIKit/UIKit.h>
#class Quiz;
#interface ViewController : UIViewController
#property (nonatomic, assign) NSInteger quizIndex;
#property (nonatomic, strong) Quiz* quiz;
#property (nonatomic, weak) IBOutlet UILabel* questionLabel;
#property (nonatomic, weak) IBOutlet UILabel* answer1Label;
#property (nonatomic, weak) IBOutlet UILabel* answer2Label;
#property (nonatomic, weak) IBOutlet UILabel* answer3Label;
#property (nonatomic, weak) IBOutlet UIButton* answer1Button;
#property (nonatomic, weak) IBOutlet UIButton* answer2Button;
#property (nonatomic, weak) IBOutlet UIButton* answer3Button;
#property (nonatomic, weak) IBOutlet UIImageView* movie1;
#property (nonatomic, weak) IBOutlet UIImageView* movie2;
#property (nonatomic, weak) IBOutlet UIImageView* movie3;
#property (nonatomic, weak) IBOutlet UILabel* statusLabel;
#property (nonatomic, weak) IBOutlet UIButton* startButton;
#property (nonatomic, weak) IBOutlet UIButton* infoButton;
#end
.m
#import "ViewController.h"
#import "Quiz.h"
#interface ViewController ()
#property (nonatomic, assign) NSInteger answer;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.quizIndex = 999;
self.quiz = [[Quiz alloc] initWithQuiz:#"Quotes"];
self.questionLabel.backgroundColor = [UIColor colorWithRed:51/255.0 green:133/255.0 blue:238/255.0 alpha:1.0];
[self nextQuizItem];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
-(void) nextQuizItem {
if(self.quizIndex == 999){
self.quizIndex = 0;
self.statusLabel.text = #"";
}else if((self.quiz.quizCount-1) > self.quizIndex) {
self.quizIndex++;
}else{
self.quizIndex = 0;
self.statusLabel.text = #"";
}
if(self.quiz.quizCount >= (self.quizIndex+1)){
self.questionLabel.text = self.quiz.quote;
self.answer1Label.text = self.quiz.ans1;
self.answer2Label.text = self.quiz.ans2;
self.answer3Label.text = self.quiz.ans3;
}else{
self.quizIndex = 0;
[self quizDone];
}
self.answer1Label.backgroundColor = [UIColor colorWithRed:51/255.0 green:133/255.0 blue:238/255.0 alpha:1.0];
self.answer2Label.backgroundColor = [UIColor colorWithRed:51/255.0 green:133/255.0 blue:238/255.0 alpha:1.0];
self.answer3Label.backgroundColor = [UIColor colorWithRed:51/255.0 green:133/255.0 blue:238/255.0 alpha:1.0];
self.answer1Button.hidden = NO;
self.answer2Button.hidden = NO;
self.answer3Button.hidden = NO;
}
-(void) quizDone {
}
#end
And my Quiz.h:
#import <Foundation/Foundation.h>
#interface Quiz : NSObject
#property (nonatomic, strong) NSMutableArray* movieArray;
#property (nonatomic, assign) NSInteger correctCount;
#property (nonatomic, assign) NSInteger incorrectCount;
#property (nonatomic, assign) NSInteger quizCount;
#property (nonatomic, readonly, strong) NSString* quote;
#property (nonatomic, readonly, strong) NSString* ans1;
#property (nonatomic, readonly, strong) NSString* ans2;
#property (nonatomic, readonly, strong) NSString* ans3;
-(id)initWithQuiz:(NSString*)plistName;
-(void)nextQuestion:(NSUInteger)idx;
-(BOOL)checkQuestion:(NSUInteger)question forAnswer:(NSUInteger)answer;
#end
and Quiz.m:
#import "Quiz.h"
#interface Quiz()
#property (nonatomic, strong) NSString* quote;
#property (nonatomic, strong) NSString* ans1;
#property (nonatomic, strong) NSString* ans2;
#property (nonatomic, strong) NSString* ans3;
#end
#implementation Quiz
-(id)initWithQuiz:(NSString *)plistName {
if((self=[super init])) {
NSString* plistCatPath = [[NSBundle mainBundle] pathForResource:plistName ofType:#"plist"];
self.movieArray = [NSMutableArray arrayWithContentsOfFile:plistCatPath];
self.quizCount = [self.movieArray count];
}
return self;
}
-(void)nextQuestion:(NSUInteger)idx {
self.quote = [NSString stringWithFormat:#"%#", self.movieArray[idx][#"quote"]];
self.ans1 = self.movieArray[idx][#"ans1"];
self.ans2 = self.movieArray[idx][#"ans2"];
self.ans3 = self.movieArray[idx][#"ans3"];
if(idx == 0){
self.correctCount = 0;
self.incorrectCount = 0;
}
}
-(BOOL)checkQuestion:(NSUInteger)question forAnswer:(NSUInteger)answer {
NSString* ans = [NSString stringWithFormat:#"%#", self.movieArray[question][#"answer"]];
if([ans intValue] == answer) {
self.correctCount++;
return YES;
}else{
self.incorrectCount++;
return NO;
}
}
#end
I am using the latest Xcode 6, the outlets are connected, and the plist file I am using has the name "Quotes.plist".
Any help or suggestion would be greatly appreciated.
Thank you.
You are not getting the next quiz question before displaying it.
Add the following to the second if statement of nextQuizItem in ViewController.m:
[self.quiz nextQuestion:self.quizIndex];
so that it looks like this
if(self.quiz.quizCount >= (self.quizIndex+1)){
[self.quiz nextQuestion:self.quizIndex]; // <-- ADD THIS
self.questionLabel.text = self.quiz.quote;
self.answer1Label.text = self.quiz.ans1;
self.answer2Label.text = self.quiz.ans2;
self.answer3Label.text = self.quiz.ans3;
}else{
self.quizIndex = 0;
[self quizDone];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated]
[[self.locationManager startRangingBeaconsInRegion: self.rangedRegion]
]}
- (void)viewDidDisappear:(BOOL)animated {
[super viewDidAppear:animated]
[[self.locationManager stopRangingBeaconsInRegion: self.rangedRegion]
]}
- (void)viewDidLoad
{
[super viewDidLoad];
self.tagname.text = [self.TagDetail objectForKey:#"Name"];
self.title = [self.TagDetail objectForKey:#"Name"];
NSString *proximity = #"near";
if ([beacon.proximity == CLProximityNear:]) {
NSLog(#"Show near");
[self.near setHidden:(NO)];
[self.far setHidden:(YES)];
[self.immediate setHidden:(YES)];
}
else if ([beacon.proximity == CLProximityFar]) {
NSLog(#"Show");
[self.near setHidden:(YES)];
[self.far setHidden:(NO)];
[self.immediate setHidden:(YES)];
}
else if ([beacon.proximity == CLProximityImmediate]) {
NSLog(#"Show");
[self.near setHidden: (YES)];
[self.far setHidden: (YES)];
[self.immediate setHidden: (NO)];
}
Heres the .h file
#interface TagDetailViewController : UIViewController
#property (nonatomic, strong) PFObject* TagDetail;
#property (strong, nonatomic)IBOutlet UILabel *tagname;
#property (nonatomic, strong)IBOutlet UIImageView *immediate;
#property (nonatomic, strong)IBOutlet UIImageView *near;
#property (nonatomic, strong)IBOutlet UIImageView *far;
#property (nonatomic, strong)IBOutlet UIButton *showOnMap;
#property CLLocationManager *locationManager;
#property CLBeaconRegion *rangedRegion;
#property NSUUID *artemisUUID;
#property CLBeaconMajorValue *major;
#property CLBeaconMinorValue *minor;
#property CLBeacon *proximity;
#end
Its telling me that the variable beacon is not identified, but doing so in the header file as *beacon doesn't remove any errors. What am I doing wrong? Im really new to this language so that is a large part of why Im running into errors, but I cant figure this one out.
You don't have a variable named beacon, you have a CLBeacon object named proximity.
I have the following app that I've implemented a scroll view (image outlines hierarchy on storyboard:e
I have turned off Auto Layout, as severe posts here seem to indicate this creates a problem.
here is my .h file:
#import <UIKit/UIKit.h>
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <AssetsLibrary/ALAsset.h>
#import <AssetsLibrary/ALAssetRepresentation.h>
#import <ImageIO/CGImageSource.h>
#import <ImageIO/CGImageProperties.h>
#import <CoreLocation/CoreLocation.h>
#interface DobInAHoonViewController : UIViewController<UIImagePickerControllerDelegate, UINavigationControllerDelegate, MFMailComposeViewControllerDelegate, UITextFieldDelegate, CLLocationManagerDelegate>
{
IBOutlet UIPickerView *vehiclePickerView;
UIImagePickerController *picker1;
UIImagePickerController *picker2;
UIImage *image;
IBOutlet UIImageView *imageView;
CLLocationManager *locationManager;
CGFloat animatedDistance;
}
#property (strong, nonatomic) IBOutlet UIImageView *BackgroundImage;
#property (strong, nonatomic) IBOutlet UIScrollView *scrollView;
#property (strong, nonatomic) NSString *Latitude;
#property (strong, nonatomic) NSString *longditute;
#property (strong, nonatomic) NSArray *toRecipients;
#property (strong, nonatomic) ALAssetsLibrary *assetsLibrary;
#property (strong, nonatomic) NSMutableArray *groups;
#property (nonatomic, retain) IBOutlet UITextField *vehilceMake;
#property (nonatomic, retain) IBOutlet UITextField *vehilceColour;
#property (nonatomic, retain) IBOutlet UITextField *regoNumber;
#property (nonatomic, retain) IBOutlet UITextField *location;
#property (nonatomic, retain) IBOutlet UITextField *additionalInfo;
#property (nonatomic, retain) IBOutlet UITextField *vehicleType;
- (IBAction)takePhoto;
-(IBAction)chooseExisting;
-(IBAction)actionEmailComposer;
-(IBAction)textFileReturn:(id)sender;
-(IBAction)DismissKeyboard:(id)sender;
#end
and my .m file:
#import "DobInAHoonViewController.h"
#interface DobInAHoonViewController ()
#end
#implementation DobInAHoonViewController
#synthesize BackgroundImage;
#synthesize vehilceColour;
#synthesize vehilceMake;
#synthesize regoNumber;
#synthesize Latitude;
#synthesize location;
#synthesize longditute;
#synthesize additionalInfo;
#synthesize toRecipients;
#synthesize assetsLibrary;
#synthesize groups;
#synthesize vehicleType;
#synthesize scrollView;
static const CGFloat KEYBOARD_ANIMATION_DURATION = 0.3;
static const CGFloat MINIMUM_SCROLL_FRACTION = 0.2;
static const CGFloat MAXIMUM_SCROLLFRACTION = 0.8;
static const CGFloat PORTRAIT_KEYBOARD_HEIGHT = 216;
static const CGFloat LANDSCAPE_KEYBOARD_HEIGHT = 140;
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[vehilceMake resignFirstResponder];
[vehilceMake resignFirstResponder];
[vehilceColour resignFirstResponder];
[location resignFirstResponder];
[additionalInfo resignFirstResponder];
[regoNumber resignFirstResponder];
return 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.
[self.scrollView setScrollEnabled:YES];
[self.scrollView setContentSize:(CGSizeMake(320, 1000))];
locationManager = [[CLLocationManager alloc]init];
toRecipients = [[NSArray alloc]initWithObjects:#"scott.boon#shellharbour.nsw.gov.au", nil];
BackgroundImage.alpha = 0.3;
static int emergAlertCounter;
if (emergAlertCounter <1) {
UIAlertView *emergencyAlert = [[UIAlertView alloc]
initWithTitle:#"NOTE" message:#"Do not endanger your life to dob in a hoon. If your life is threatened, or you are reporting an emergency situation, exit this app and dial '000' IMMEDIATLY!" delegate:nil cancelButtonTitle:#"OK" otherButtonTitles:nil];
[emergencyAlert show];
emergAlertCounter = emergAlertCounter+1;
}
}
for some reason, when i run the app, the scroll view is not scrolling. I have set the property to scroll in the interface builder.
Any suggestions would be greatly appreciated.
You'll have to make sure your scroll view is smaller than 320 x 1000 and that the embedded view is larger than 320 x 1000
I made app for iOS6, and as I run this on iOS7, so it disturbed my design outlook.
My Cell Code is as followed.
#interface Cell_ClubOffer : UITableViewCell
#property (nonatomic, retain) IBOutlet UILabel *titleName;
#property (nonatomic, retain) IBOutlet UILabel *description;
#property (nonatomic, retain) IBOutlet UILabel *distance;
#property (nonatomic, retain) IBOutlet UIImageView *cellImageView;
#property(nonatomic,retain)IBOutlet UIButton *plainButton;
#property (nonatomic, retain) IBOutlet UIImageView *backImageView;
#end
#implementation Cell_ClubOffer
#synthesize cellImageView;
#synthesize plainButton;
#synthesize backgroundView;
#synthesize backImageView;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
// Initialization code
}
return self;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
[super setSelected:selected animated:animated];
// Configure the view for the selected state
}
#end
Now my Cell Looks like as in iOS6
and on iOS7, it looks bad, like
What is proper way to migrate it from iOS6 to iOS7, simply.
My view controller contains an UIImageView and a UITextView below. This is my code:
#import <UIKit/UIKit.h>
#interface DescriptionViewController : UIViewController
#property (nonatomic, strong) IBOutlet UIImageView *descriptionImage;
#property (nonatomic, strong) IBOutlet UITextView *descriptionText;
#property (nonatomic, strong) NSString *text;
#property (nonatomic, strong) NSString *image;
#end
#interface DescriptionViewController ()
#end
#implementation DescriptionViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.descriptionText = [[UITextView alloc] init];
self.descriptionImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.image]]];
self.descriptionText.text = self.text;
NSLog(#"%#", self.descriptionText.text);
}
#end
My code doesn't show up in textview, but I can print it using NSLog.
What could be the problem ?
You're initializing a new UITextView here.-
self.descriptionText = [[UITextView alloc] init];
This way, you get a new UITextView not linked with the main view. Remove that line and make sure descriptionText is properly linked in your xib or storyboard.
However, if your intention was actually programmatically creating a new UITextView from scratch, then you'll also have to programmatically add it to your main view.