In objective-c, by using self in a view controller, does this create a strong reference cycle? - objective-c

I've noticed while working with Objective-C, the compiler throws error symbols enforcing the use of either self or an underscore when using a property which I think doesn't happen as harshly when using Swift. I'm now at the crossroads where I believe my view controller isn't being deallocated from the navigation stack. I've used self pretty heavily in order to silence the error symbols but I don't know if this has created retain cycles or not?
#import "MenuController.h"
#import "AppDelegate.h"
#import "CMUser.h"
#import "NotificationsSettingsController.h"
#import "PersonalInfoController.h"
#import "BeLive-Swift.h"
#import "LoginAndSecurityController.h"
#import "SplashController.h"
#define kCellSeparatorTag 100
#implementation SideMenuCell
- (void)layoutSubviews{
[super layoutSubviews];
}
#end
#interface MenuController ()<UITableViewDataSource ,UITableViewDelegate>
#property (nonatomic, strong) NSArray *menuTitles;
#property (nonatomic, weak) IBOutlet UITableView *tableView;
#property (weak, nonatomic) IBOutlet UILabel *usersNameLabel;
#property (weak, nonatomic) IBOutlet UILabel *usersEmailLabel;
#property (weak, nonatomic) IBOutlet UILabel *addPhotoLabel;
#property (weak, nonatomic) IBOutlet SettingsHeaderViewWithPhoto *settingsHeaderViewWithPhoto;
#end
#implementation MenuController
NSString *userFirstNameString;
NSString *userLastNameString;
NSString *userEmailString;
NSMutableString * usersFullNameString;
+ (instancetype)controller{
static MenuController *sharedInstance = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
sharedInstance = ControllerFromMainStoryBoard([self description]);
});
return sharedInstance;
}
- (void) viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
[self.settingsHeaderViewWithPhoto.backButton addTarget:self action:#selector(popViewCon:) forControlEvents:UIControlEventTouchUpInside];
[self.settingsHeaderViewWithPhoto.rightButton addTarget:self action:#selector(logoutTapped:) forControlEvents:UIControlEventTouchUpInside];
}
-(void)showUserInfoInHeaderView{
if (userEmailString.length != 0) {
self.settingsHeaderViewWithPhoto.emailSubLabel.text = userEmailString;
} else {
self.settingsHeaderViewWithPhoto.emailSubLabel.text = #"Email not available";
}
if (usersFullNameString.length != 0) {
self.settingsHeaderViewWithPhoto.nameLabel.text = usersFullNameString;
} else {
self.settingsHeaderViewWithPhoto.nameLabel.text = #"Full name not available";
}
[self setMenuArrayBasedOnUserType];
[self.tableView reloadData];
}
-(void)setMenuArrayBasedOnUserType{
if (CMUser.currentUser.type == UserTypeArtist) {
self.menuTitles = #[#[#"Notifications", #"Stripe Info", #"Personal Info", #"Login and Security", #"Invite a Friend", #"Help", #"Legal Agreement"]];
[self.tableView reloadData];
} else if (CMUser.currentUser.type == UserTypeViewer){
self.menuTitles = #[#[#"Notifications", #"Personal Info", #"Login and Security", #"Invite a Friend", #"Help", #"Legal Agreement"]];
[self.tableView reloadData];
} else {
[self showLoggedInAlert];
};
}
-(void)showLoggedInAlert{
UIAlertController *alert = [UIAlertController alertControllerWithTitle:#"Session Time Out"
message:#"You must be logged in first."
preferredStyle:UIAlertControllerStyleAlert]; // 1
UIAlertAction *firstAction = [UIAlertAction actionWithTitle:#"OK"
style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
NSLog(#"You pressed ok");
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
SplashController *vc = [sb instantiateViewControllerWithIdentifier:#"SplashController"];
[self presentViewController:vc animated:YES completion:nil];
}];
[alert addAction:firstAction];
[self presentViewController:alert animated:YES completion:nil];
}
-(void)getLoggedInUserInfo{
[NetworkManager callEndPoint:USER_DETAILS withDict:nil method:#"GET" JSON:YES success:^(id responseObject) {
userFirstNameString = responseObject[#"data"][0][#"firstname"];
userLastNameString = responseObject[#"data"][0][#"lastname"];
userEmailString = responseObject[#"data"][0][#"emailAddress"];
usersFullNameString = [NSMutableString stringWithString:userFirstNameString];
[usersFullNameString appendString:#" "];
[usersFullNameString appendString: userLastNameString];
[self showUserInfoInHeaderView];
} failure:^(id responseObject, NSError *error) {
NSLog(#"callEndPoint error is: %#", error);
}];
}
-(void)showProfilPhoto{
if ([CMUser currentUser][#"imageUrl"] != NULL) {
self.settingsHeaderViewWithPhoto.addPhotoLabel.text = #"";
[self.settingsHeaderViewWithPhoto.userPhotoButton sd_setImageWithURL:[NSURL URLWithString:[CMUser currentUser][#"imageUrl"]]forState:UIControlStateNormal];
} else {
self.settingsHeaderViewWithPhoto.addPhotoLabel.text = #"Add Photo";
}
}
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationBarHidden = YES;
[self.tableView registerClass:[SettingsTableViewCell class] forCellReuseIdentifier:#"MenuCell"];
self.tableView.backgroundColor = [UIColor colorWithHex:0x222222];
[self getLoggedInUserInfo];
[self showProfilPhoto];
}
- (void)viewWillLayoutSubviews{
[super viewWillLayoutSubviews];
CGFloat revealWidth = ApplicationDelegate.drawerController.maximumLeftDrawerWidth;
CGRect frame = self.view.frame;
frame.size.width = revealWidth;
self.view.frame = frame;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
return self.menuTitles.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
NSMutableArray *menuItems = self.menuTitles[section];
return menuItems.count;
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
NSArray *menuItems = self.menuTitles[indexPath.section];
SettingsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:#"MenuCell"
forIndexPath:indexPath];
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.textLabel.text = menuItems[indexPath.row];
UIView *separatorView = [cell.contentView viewWithTag:kCellSeparatorTag];
if (!separatorView) {
CGFloat revealWidth = ApplicationDelegate.drawerController.maximumLeftDrawerWidth;
separatorView = [[UIView alloc] initWithFrame:CGRectMake(10, 43, revealWidth - 20, 1)];
separatorView.backgroundColor = [UIColor darkGrayColor];
[cell.contentView addSubview:separatorView];
}
return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
[tableView deselectRowAtIndexPath:indexPath
animated:YES];
if (indexPath.section == 0) {
switch (indexPath.row) {
case 0:
[self notificationsTapped];
default:
break;
}
switch (indexPath.row) {
case 1:
//TODO: IF userrole == artist then show stripe info else if userrole == , show person information
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show strip credit info");
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show personal info");
[self personalInfoTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 2:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show strip personal info");
[self personalInfoTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show login and security");
[self loginSecurityTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 3:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show login and security");
[self loginSecurityTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show Invite a friend");
[self inviteFriendTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 4:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show Invite a friend");
[self inviteFriendTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show Help");
//goto: webURL www.belive.com/help
[self helpTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 5:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show help");
//goto: webURL www.belive.com/help
[self helpTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"Show legal agreement");
//goto: webURL www.belive.com/legal
[self legalAgreementTapped];
}
break;
default:
break;
}
switch (indexPath.row) {
case 6:
if (CMUser.currentUser.type == UserTypeArtist) {
NSLog(#"Show legal agreement");
//goto: webURL www.belive.com/legal
[self legalAgreementTapped];
} else if (CMUser.currentUser.type == UserTypeViewer) {
NSLog(#"do nothing because there is not a value for this case.");
}
break;
default:
break;
}
}
}
- (void)notificationsTapped{
NotificationsSettingsController *vc = [NotificationsSettingsController controller];
[self.navigationController pushViewController:vc
animated:YES];
}
- (void)popViewCon: (UIButton*)sender{
[ApplicationDelegate toggleMenu];
}
- (void)logoutTapped: (UIButton*)sender{
[ApplicationDelegate toggleMenu];
[CMUser logOut];
[ApplicationDelegate setController];
}
-(void)personalInfoTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
PersonalInfoController *vc = [sb instantiateViewControllerWithIdentifier:#"PersonalInfoController"];
vc.usersProfilePhoto = self.settingsHeaderViewWithPhoto.userPhotoButton.imageView.image;
[self.navigationController pushViewController:vc animated:YES];
}
-(void)loginSecurityTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
LoginAndSecurityController *vc = [sb instantiateViewControllerWithIdentifier:#"LoginAndSecurityController"];
//LoginAndSecurityController *vc = [LoginAndSecurityController controller];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)inviteFriendTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
InviteFriendController
*vc = [sb instantiateViewControllerWithIdentifier:#"InviteFriendController"];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)helpTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
HelpController *vc = [sb instantiateViewControllerWithIdentifier:#"helpController"];
[self.navigationController pushViewController:vc animated:YES];
}
-(void)legalAgreementTapped{
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Settings" bundle:nil];
LegalAgreementController *vc = [sb instantiateViewControllerWithIdentifier:#"legalController"];
[self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)addPhotoButtonTapped:(UIButton *)sender {
[self configureAddPhotoActionSheet];
}
-(void)configureAddPhotoActionSheet{
UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
// Cancel button tappped do nothing.
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Take a Photo" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// take photo button tapped.
[self takePhoto];
}]];
[actionSheet addAction:[UIAlertAction actionWithTitle:#"Choose Photo from Library" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
// choose photo button tapped.
[self selectPhoto];
}]];
[self presentViewController:actionSheet animated:YES completion:nil];
}
- (void)takePhoto{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
[self presentViewController:picker animated:YES completion:NULL];
}
- (void)selectPhoto{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:picker animated:YES completion:NULL];
}
//UIImagePickerDelegate Methods
/*
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
[self.profilePictureButton setImage:chosenImage forState:UIControlStateNormal];
self.addPhotoLabel.text = #"";
[self updateProfilePicture:chosenImage];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
[picker dismissViewControllerAnimated:YES completion:NULL];
} */
- (void)updateProfilePicture:(UIImage*)image{
NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
NSString *imageString = [NSString stringWithFormat:#"data:image/png;base64,%#", [imageData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength]];
NSDictionary *params = #{#"ImageFileContent" : imageString,
#"ImageName" : #"image.jpg"
};
NSString *uploadEndPoint = #"artist/add-artist-photo";
MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.settingsHeaderViewWithPhoto.userPhotoButton animated:YES];
[NetworkManager callEndPoint:uploadEndPoint
withDict:params
method:#"POST"
JSON:YES
success:^(id responseObject) {
NSLog(#"The response object has: %#", responseObject);
[CMUser currentUser][#"imageUrl"] = responseObject[#"data"][0][#"imageURL"];
[[CMUser currentUser] setCurrent];
[HUD hideAnimated:YES];
} failure:^(id responseObject, NSError *error) {
[HUD hideAnimated:YES];
}];
}
#end

This is used to synthesise the variable. It is necessary in objective c and swift works differently then swift. And it doesn't creat any retail cycle.

After doing some reading, I've come to the realization that retain cycles can be created by delegates, using self within a closure, and if other objects are referencing your view controller in question. The problem I had was that this particular view controller is actually the root view controller of a navigation stack in which other view controllers were referencing it as well. Also I made a call to self within two block statements in the code above which also has the potential for creating a strong reference to itself.

Related

Unrecognized selector issue

This is the code:
#interface CreateAccountViewController : UIViewController<UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate>
{
UIToolbar *keyboardToolbar;
NSDate *birthdate;
UIActionSheet *dateSheet;
}
#property(strong, nonatomic) IBOutlet UITextField *txtFirstName;
#property(strong, nonatomic) IBOutlet UITextField *txtLastName;
#property(strong, nonatomic) IBOutlet UITextField *txtGender;
#property(strong, nonatomic) IBOutlet UITextField *txtBirthdate;
#property(strong, nonatomic) IBOutlet UITextField *txtEmail;
-(void)resignKeyboard:(id)sender;
-(void)gotoPreviousField:(id)sender;
-(void)gotoNextField:(id)sender;
-(void)setBirth;
-(void)dismissDateSet;
-(void)gotoPreviousBirthdate;
-(void)gotoNextBirthdate;
#end
#implementation CreateAccountViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
[[[keyboardToolbar items] objectAtIndex:0] setEnabled:YES];
[[[keyboardToolbar items] objectAtIndex:1] setEnabled:YES];
if([textField isEqual:self.txtFirstName])
{
[[[keyboardToolbar items] objectAtIndex:0] setEnabled:NO];
}
else if([textField isEqual:self.txtEmail])
{
[[[keyboardToolbar items] objectAtIndex:0] setEnabled:NO];
}
}
-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
if([textField isEqual:self.txtBirthdate])
{
[self setBirth];
return NO;
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view
if(keyboardToolbar==nil)
{
keyboardToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *btnPrevious = [[UIBarButtonItem alloc] initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(gotoPreviousField:)];
UIBarButtonItem *btnNext = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(gotoNextField:)];
UIBarButtonItem *btnExtraSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *btnDone=[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(resignKeyboard:)];
[keyboardToolbar setItems:[[NSArray alloc] initWithObjects:btnPrevious, btnNext, btnExtraSpace, btnDone, nil]];
}
}
-(void) resignKeyboard:(id)sender
{
if([self.txtFirstName isFirstResponder])
{
[self.txtFirstName resignFirstResponder];
}
else if([self.txtLastName isFirstResponder])
{
[self.txtLastName resignFirstResponder];
}
else if([self.txtGender isFirstResponder])
{
[self.txtGender resignFirstResponder];
}
else if([self.txtBirthdate isFirstResponder])
{
[self.txtBirthdate resignFirstResponder];
}
else if([self.txtEmail isFirstResponder])
{
[self.txtEmail resignFirstResponder];
}
}
-(void) gotoPreviousField:(id)sender
{
if([self.txtFirstName isFirstResponder])
{
[self.txtEmail becomeFirstResponder];
}
else if([self.txtLastName isFirstResponder])
{
[self.txtFirstName becomeFirstResponder];
}
else if([self.txtGender isFirstResponder])
{
[self.txtLastName becomeFirstResponder];
}
else if([self.txtEmail isFirstResponder])
{
[self.txtBirthdate becomeFirstResponder];
}
}
-(void)gotoNextField:(id)sender
{
if([self.txtFirstName isFirstResponder])
{
[self.txtLastName becomeFirstResponder];
}
else if([self.txtLastName isFirstResponder])
{
[self.txtGender becomeFirstResponder];
}
else if([self.txtGender isFirstResponder])
{
[self.txtBirthdate becomeFirstResponder];
}
else if([self.txtEmail isFirstResponder])
{
[self.txtFirstName becomeFirstResponder];
}
}
-(void)setBirth
{
dateSheet=[[UIActionSheet alloc] initWithTitle:nil delegate:nil cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
[dateSheet setActionSheetStyle:UIActionSheetStyleDefault];
CGRect pickerFrame=CGRectMake(0, 44, 0, 0);
UIDatePicker *birthDayPicker=[[UIDatePicker alloc] initWithFrame:pickerFrame];
[birthDayPicker setDatePickerMode:UIDatePickerModeDate];
[dateSheet addSubview:birthDayPicker];
UIToolbar *birthDayToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, dateSheet.bounds.size.width, 44)];
[birthDayToolbar setBarStyle:UIBarStyleBlackTranslucent];
[birthDayToolbar sizeToFit];
UIBarButtonItem *btnPrevious = [[UIBarButtonItem alloc] initWithTitle:#"Previous" style:UIBarButtonItemStyleBordered target:self action:#selector(gotoPreviousBirthdate:)];
UIBarButtonItem *btnNext = [[UIBarButtonItem alloc] initWithTitle:#"Next" style:UIBarButtonItemStyleBordered target:self action:#selector(gotoNextBirthdate:)];
UIBarButtonItem *btnExtraSpace= [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
UIBarButtonItem *btnDone=[[UIBarButtonItem alloc] initWithTitle:#"Done" style:UIBarButtonItemStyleDone target:self action:#selector(dismissDateSet:)];
[birthDayToolbar setItems:[[NSArray alloc] initWithObjects:btnPrevious, btnNext, btnExtraSpace, btnDone, nil]];
[dateSheet addSubview:birthDayToolbar];
[dateSheet showInView:self.view];
[dateSheet setBounds:CGRectMake(0, 0, 320, 485)];
}
-(void)dismissDateSet
{
NSArray *listOfViews = [dateSheet subviews];
for(UIView *subView in listOfViews)
{
if([subView isKindOfClass:[UIDatePicker class]])
{
NSDateFormatter *dateFormatter=[[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:#"MM/dd/yyyy"];
self.txtBirthdate.text=[dateFormatter stringFromDate:[(UIDatePicker*)subView date]];
}
}
}
-(void)gotoPreviousBirthdate
{
[self dismissDateSet];
[self.txtGender becomeFirstResponder];
}
-(void)gotoNextBirthdate
{
[self dismissDateSet];
[self.txtEmail becomeFirstResponder];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return 5;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CreateCellIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
UITextField *textField = [[UITextField alloc] init];
textField.enablesReturnKeyAutomatically = YES;
textField.autocorrectionType = UITextAutocorrectionTypeNo;
textField.autocapitalizationType = UITextAutocapitalizationTypeNone;
textField.clearButtonMode=UITextFieldViewModeWhileEditing;
textField.delegate=self;
CGRect aRect=CGRectMake(10, 10.f, CGRectGetWidth(cell.bounds)-40.f, 30.f );
textField.frame = aRect;
if(indexPath.row==0)
{
textField.placeholder = #"First name";
self.txtFirstName=textField;
}
else if(indexPath.row==1)
{
textField.placeholder = #"Last name";
self.txtLastName=textField;
}
else if(indexPath.row==2)
{
textField.placeholder = #"Gender";
self.txtGender=textField;
}
else if(indexPath.row==3)
{
textField.placeholder = #"Date of birth";
self.txtBirthdate=textField;
}
else
{
textField.placeholder = #"Email";
self.txtEmail=textField;
}
[cell.contentView addSubview:textField];
cell.selectionStyle=UITableViewCellSelectionStyleNone;
textField.inputAccessoryView=keyboardToolbar;
return cell;
}
#end
If i press the Previous button from the date picker toolbar i get this exception:
[CreateAccountViewController gotoPreviousBirthdate:]: unrecognized selector sent to instance
If i press the Next button from the date picker toolbar i get this exception:
[CreateAccountViewController gotoNextBirthdate:]: unrecognized selector sent to instance
If i press the Done button from the date picker toolbar i get this exception:
[CreateAccountViewController dismissDateSet:]: unrecognized selector sent to instance
It seems that i am making the same mistake 3 times but i can't quite figure out what is that.
Thanks
Your setting the target selector to be:
#selector(gotoPreviousBirthdate:)
But you declare it as:
-(void)gotoPreviousBirthdate;
Remove the : in the selector as #selector(gotoPreviousBirthdate) since you're not taking an argument, or change the declaration to be -(void)gotoPreviousBirthday:(id)sender.
Then repeat for the other two.
You are calling the methods as if they are class methods, but they are declared as instance methods in your interface. Also the invocation of the selector is incorrect.
Create an instance of the class first:
CreateAccountViewController *vc = [[CreateAccountViewController alloc] init]; //Something like this
Then you can call the instance methods:
[vc gotoPreviousBirthday]; //Note no trailing colon!
Or, if you declared the method as
+(void)gotoPreviousBirthdate;
note the + in the signature - then you could call the method as your code shows.
This is a pretty fundamental concept in Objective-C so I would recommend you read the documentation first. Apple Objective-C Primer

popViewControllerAnimated affects viewwillappear?

I have two classes. lets say A,B Classes. from A class pushviewController is called, then B class will appear. Then here is the problem .when i call popviewcontrolleranimated method from B class it is going back to A, but then both class`s viewwillappear method is being called. so anyone can tell me what is going on in here. i am stuck!.
Below is the A class.
#implementation ShakeViewController
- (id) init {
if (self = [super init]) {
movieName = #"04";
self.view = [[[UIView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
[self.view setBackgroundColor:[UIColor whiteColor]];
UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"shake_it.png"]];
[self.view addSubview:imageView];
[imageView release];
nextController = [[OtsugeViewController alloc] init];
}
return self;
}
- (void)accelerometer:(UIAccelerometer *)accelerometer didAccelerate:(UIAcceleration *)acceleration {
const float violence = 1.2;
static BOOL beenhere;
BOOL shake = FALSE;
if (beenhere) return;
beenhere = TRUE;
if (acceleration.x > violence || acceleration.x < (-1* violence))
shake = TRUE;
if (acceleration.y > violence || acceleration.y < (-1* violence))
shake = TRUE;
if (acceleration.z > violence || acceleration.z < (-1* violence))
shake = TRUE;
if (shake && mPlayerPushed) {
[self playSound:#"suzu"];
if ([[NSUserDefaults standardUserDefaults] boolForKey:#"noVib"] == NO) {
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);
}
[[UIAccelerometer sharedAccelerometer] setDelegate:nil];
[self presentModalViewController:mMoviePlayer animated:YES];
[self play];
mPlayerPushed = YES;
}
beenhere = false;
}
- (void)toNext {
NSLog(#"ShakeViewController:toNext");
[self.navigationController pushViewController:nextController animated:NO];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(#"Shake:viewWillAppear");
}
- (void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
[[UIAccelerometer sharedAccelerometer] setDelegate:self];
[[UIAccelerometer sharedAccelerometer] setUpdateInterval:0.5];
}
- (void)dealloc {
[super dealloc];
}
#end
here is B class
#implementation OtsugeViewController
- (id) init {
if (self = [super init]) {
movieName = #"03";
self.view = [[[OtsugeView alloc] initWithFrame:[[UIScreen mainScreen] applicationFrame]] autorelease];
}
return self;
}
- (void) toNext {
NSLog(#"OtsugeViewController:toNext");
[self.navigationController popViewControllerAnimated:NO];
}
- (void) toToppage
{
[[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:NO];
[self.navigationController popToRootViewControllerAnimated:NO];
}
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
NSLog(#"Screen touch Otsuge View");
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil
delegate:self
cancelButtonTitle:#"Cancel" destructiveButtonTitle:nil
otherButtonTitles:#"Retry", #"Main Menu", nil];
actionSheet.actionSheetStyle = UIActionSheetStyleBlackTranslucent;
actionSheet.cancelButtonIndex = 0;
[actionSheet showInView:self.view];
[actionSheet release];
}
- (void) actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex: (NSInteger)buttonIndex
{
switch (buttonIndex) {
case 0: // Retry
[self presentModalViewController:mMoviePlayer animated:YES];
[self play];
break;
case 1: // Main Menu
[self toToppage];
break;
case 2: // Cancel
break;
default:
break;
}
}
- (void) viewWillAppear:(BOOL)animated {
mMoviePlayer.moviePlayer.backgroundView.backgroundColor = [UIColor blackColor];
[self playSound:#"taiko_1"];
NSLog(#"Otsuge:viewWillAppear");
[(OtsugeView *)self.view renewImageView];
[super viewWillAppear:animated];
}
- (void) viewDidAppear:(BOOL)animated{
NSLog(#"Otsuge:viewDidAppear");
[super viewDidAppear:animated];
}
- (void) dealloc {
[super dealloc];
}
#end
It's normal. View will appear is called each time a view will appear. If you want to call a method only when the view appears for the first time use -viewdidload because in a view controller each view that is in the stack is kept in memory but those you pop get deallocated.

No known class method for selector 'sharedStore'

Have a singleton class for BNRItemStore, but when I tried to call it, I get the above error which causes an ARC issue. Have commented out the error.
DetailViewController.m
#import "DetailViewController.h"
#import "BNRItem.h"
#import "BNRImageStore.h"
#import "BNRItemStore.h"
#implementation DetailViewController
#synthesize item;
-(id)initForNewItem:(BOOL)isNew
{
self = [super initWithNibName:#"DetailViewController" bundle:nil];
if(self){
if (isNew) {
UIBarButtonItem *doneItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:#selector(save:)];
[[self navigationItem] setRightBarButtonItem:doneItem];
UIBarButtonItem *cancelItem = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
target:self
action:#selector(cancel:)];
[[self navigationItem] setLeftBarButtonItem:cancelItem];
}
}
return self;
}
-(id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)bundle
{
#throw [NSException exceptionWithName:#"Wrong initializer"
reason:#"Use initForNewItem:"
userInfo:nil];
return nil;
}
-(void)viewDidLoad
{
[super viewDidLoad];
UIColor *clr = nil;
if ([[UIDevice currentDevice]userInterfaceIdiom]== UIUserInterfaceIdiomPad) {
clr = [UIColor colorWithRed:0.875 green:0.88 blue:0.91 alpha:1];
} else {
clr = [UIColor groupTableViewBackgroundColor];
}
[[self view]setBackgroundColor:clr];
}
- (void)viewDidUnload {
nameField = nil;
serialNumberField = nil;
valueField = nil;
dateLabel = nil;
imageView = nil;
[super viewDidUnload];
}
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[nameField setText:[item itemName]];
[serialNumberField setText:[item serialNumber]];
[valueField setText:[NSString stringWithFormat:#"%d", [item valueInDollars]]];
// Create a NSDateFormatter that will turn a date into a simple date string
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
// Use filtered NSDate object to set dateLabel contents
[dateLabel setText:[dateFormatter stringFromDate:[item dateCreated]]];
NSString *imageKey = [item imageKey];
if (imageKey) {
// Get image for image key from image store
UIImage *imageToDisplay = [[BNRImageStore sharedStore]imageForKey:imageKey];
// Use that image to put on the screen in imageview
[imageView setImage:imageToDisplay];
} else {
// Clear the imageview
[imageView setImage:nil];
}
}
-(void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
// Clear first responder
[[self view]endEditing:YES];
// "Save" changes to item
[item setItemName:[nameField text]];
[item setSerialNumber:[serialNumberField text]];
[item setValueInDollars:[[valueField text] intValue]];
}
-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)io
{
if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad) {
return YES;
} else {
return (io==UIInterfaceOrientationPortrait);
}
}
-(void)setItem:(BNRItem *)i
{
item = i;
[[self navigationItem] setTitle:[item itemName]];
}
- (IBAction)takePicture:(id)sender {
if ([imagePickerPopover isPopoverVisible]) {
// If the popover is already up, get rid of it
[imagePickerPopover dismissPopoverAnimated:YES];
imagePickerPopover = nil;
return;
}
UIImagePickerController *imagePicker =
[[UIImagePickerController alloc]init];
// If our device has a camera, we want to take a picture, otherwise, we
// just pick from the photo library
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
[imagePicker setSourceType:UIImagePickerControllerSourceTypeCamera];
} else {
[imagePicker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
// This line of code will generate a warning right now, ignore it
[imagePicker setDelegate:self];
//Place image picker on the screen
// Check for iPad device before instantiating the popover controller
if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad) {
// Create a new popover controller that will display the imagepicker
imagePickerPopover = [[UIPopoverController alloc]initWithContentViewController:imagePicker];
[imagePickerPopover setDelegate:self];
// Display the popover controller; sender
// is the camera bar button item
[imagePickerPopover presentPopoverFromBarButtonItem:sender permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
} else {
[self presentViewController:imagePicker animated:YES completion:nil];
}
}
}
-(void)popoverControllerDidDismissPopover:(UIPopoverController *)popoverController
{
NSLog(#"User dismissed popover");
imagePickerPopover = nil;
}
- (IBAction)backgroundTapped:(id)sender {
[[self view]endEditing:YES];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *oldKey = [item imageKey];
// Did the item already have an image?
if (oldKey) {
// Delete the old image
[[BNRImageStore sharedStore]deleteImageForKey:oldKey];
}
UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
// Create a CFUUID object - it knows how to create unique identifier strings
CFUUIDRef newUniqueID = CFUUIDCreate(kCFAllocatorDefault);
// Create a string from unique identifier
CFStringRef newUniqueIDString = CFUUIDCreateString(kCFAllocatorDefault, newUniqueID); // Incompatible integer to pointer conversion initializing
// Use that unique ID to set our item's imageKey
NSString *key = (__bridge NSString *)newUniqueIDString;
[item setImageKey:key];
// Store image in the BNRImageStore with this key
[[BNRImageStore sharedStore] setImage:image forKey:[item imageKey]];
CFRelease(newUniqueIDString);
CFRelease(newUniqueID);
[imageView setImage:image];
if ([[UIDevice currentDevice]userInterfaceIdiom]==UIUserInterfaceIdiomPad) {
// If on the phone, the image picker is presented modally. Dismiss it.
[self dismissViewControllerAnimated:YES completion:nil];
} else {
// If on the pad, the image picker is in the popover. Dismiss the popover.
[imagePickerPopover dismissPopoverAnimated:YES];
imagePickerPopover = nil;
}
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
-(void)save:(id)sender
{
[[self presentingViewController]dismissViewControllerAnimated:YES
completion:nil];
}
-(void)cancel:(id)sender
{
// If the user cancelled, then remove the BNRItem from the store
[[BNRItemStore sharedStore]removeItem:item]; // No known class method for selector 'sharedStore'
[[self presentingViewController]dismissViewControllerAnimated:YES completion:nil];
}
DetailViewController.h
#import <UIKit/UIKit.h>
#class BNRItem;
#interface DetailViewController : UIViewController <UINavigationControllerDelegate, UIImagePickerControllerDelegate,UITextFieldDelegate, UIPopoverControllerDelegate>
{
__weak IBOutlet UITextField *nameField;
__weak IBOutlet UITextField *serialNumberField;
__weak IBOutlet UITextField *valueField;
__weak IBOutlet UILabel *dateLabel;
__weak IBOutlet UIImageView *imageView;
UIPopoverController *imagePickerPopover;
}
#property(nonatomic,strong)BNRItem *item;
-(id)initForNewItem:(BOOL)isNew;
- (IBAction)takePicture:(id)sender;
- (IBAction)backgroundTapped:(id)sender;
#end
BNRItemStore.m
#import "BNRItemStore.h"
#import "BNRItem.h"
#implementation BNRItemStore
+ (BNRItemStore *)defaultStore
{
static BNRItemStore *defaultStore = nil;
if(!defaultStore)
defaultStore = [[super allocWithZone:nil] init];
return defaultStore;
}
+ (id)allocWithZone:(NSZone *)zone
{
return [self defaultStore];
}
- (id)init
{
self = [super init];
if(self) {
allItems = [[NSMutableArray alloc] init];
}
return self;
}
- (void)removeItem:(BNRItem *)p
{
[allItems removeObjectIdenticalTo:p];
}
- (NSArray *)allItems
{
return allItems;
}
- (void)moveItemAtIndex:(int)from
toIndex:(int)to
{
if (from == to) {
return;
}
// Get pointer to object being moved so we can re-insert it
BNRItem *p = [allItems objectAtIndex:from];
// Remove p from array
[allItems removeObjectAtIndex:from];
// Insert p in array at new location
[allItems insertObject:p atIndex:to];
}
- (BNRItem *)createItem
{
BNRItem *p = [BNRItem randomItem];
[allItems addObject:p];
return p;
}
#end
BNRItemStore.h
#import <Foundation/Foundation.h>
#class BNRItem;
#interface BNRItemStore : NSObject
{
NSMutableArray *allItems;
}
+ (BNRItemStore *)defaultStore;
- (void)removeItem:(BNRItem *)p;
- (NSArray *)allItems;
- (BNRItem *)createItem;
- (void)moveItemAtIndex:(int)from
toIndex:(int)to;
#end
You are calling +sharedStore on BNRItemStore where your error occurs. This is because it really does not exist according to the code you posted.
All of the others calling +sharedStore are using the one presumably made available by BNRImageStore which you didn't provide. I assume it exists in that class? :)
In short, change:
[[BNRItemStore sharedStore]removeItem:item];
to
[[BNRImageStore sharedStore]removeItem:item];

How can I load a landscape xib using one ViewController file?

I have a universal app. I'm using separate xibs for the portrait and landscape views. I have the app detecting the orientation and changing the value of a BOOL to true when I'm in landscape. I want to know how to load my landscape xib when that BOOL is true. I've tried several different methods to achieve this, but nothing has worked. Any input on this matter would be most appreciated. I can update this post to include any code snippets necessary. Thanks in advance.
edit: I want to do all of this in one ViewController class, and only for the iPad... not the iPhone. I have all that part worked out. I just need to load the landscape xib.
edit: In my viewDidLoad I'm doing this:
if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight) {
landscape = YES;
}
Here's my main view controller .m:
#implementation PassportAmericaViewController
#synthesize browseViewButton, webView, mainView, lblMemberName, menuOpen, internetActive, hostActive, isUsingiPad, portrait, landscape;
- (void)viewDidLoad {
menuOpen = NO;
UIDevice* userDevice = [UIDevice currentDevice];
if (userDevice.userInterfaceIdiom == UIUserInterfaceIdiomPad) {
isUsingiPad = YES;
}
if (isUsingiPad)
if (userDevice.orientation == UIDeviceOrientationLandscapeLeft || userDevice.orientation == UIDeviceOrientationLandscapeRight) {
landscape = YES;
}
[self checkForKey];
[super viewDidLoad];
[self.navigationController setNavigationBarHidden:YES];
}
-(void)viewWillAppear:(BOOL)animated{
[self.navigationController setNavigationBarHidden:YES];
}
-(void) checkForKey{
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
int regCheck = [defaults integerForKey:#"registration"];
if (isUsingiPad) {
if (regCheck == 0) {
RegistrationViewController *regView = [[RegistrationViewController alloc]
initWithNibName:#"RegistrationView-iPad" bundle:[NSBundle mainBundle]];
regView.isUsingiPad = YES;
[self.navigationController pushViewController:regView animated:YES];
}else if (regCheck == 1) {
#try {
NSString *mbrFirstName = [defaults objectForKey:#"firstName"];
NSString *mbrLastName = [defaults objectForKey:#"lastName"];
NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
[name appendString:#" "];
[name appendString:mbrLastName];
lblMemberName.text = name;
}
#catch (NSException *exception) {
}
}
}else{
if (regCheck == 0) {
RegistrationViewController *regView = [[RegistrationViewController alloc]
initWithNibName:#"RegistrationView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:regView animated:YES];
}else if (regCheck == 1) {
#try {
NSString *mbrFirstName = [defaults objectForKey:#"firstName"];
NSString *mbrLastName = [defaults objectForKey:#"lastName"];
NSMutableString *name = [[NSMutableString alloc] initWithString:mbrFirstName];
[name appendString:#" "];
[name appendString:mbrLastName];
lblMemberName.text = name;
}
#catch (NSException *exception) {
}
}
}
}
-(IBAction) openBrowseView{
if (isUsingiPad && landscape) {
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:#"BrowseView-iPadLandscape" bundle:[NSBundle mainBundle]];
browseView.isUsingiPad = YES;
[self.navigationController pushViewController:browseView animated:YES];
}else if (isUsingiPad){
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:#"BrowseView-iPad" bundle:[NSBundle mainBundle]];
browseView.isUsingiPad = YES;
[self.navigationController pushViewController:browseView animated:YES];
}else{
BrowseViewController *browseView = [[BrowseViewController alloc]
initWithNibName:#"BrowseView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:browseView animated:YES];
}
}
-(IBAction) openViewMore{
if (isUsingiPad) {
ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
initWithNibName:#"ViewMoreView-iPad" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:viewMoreView animated:YES];
viewMoreView.isUsingiPad = YES;
}else{
ViewMoreViewController *viewMoreView = [[ViewMoreViewController alloc]
initWithNibName:#"ViewMoreView" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:viewMoreView animated:YES];
}
}
-(IBAction) callTollFree:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:#"tel:8002837183"]];
}
-(IBAction)clickToJoin:(id)sender {
if (isUsingiPad) {
webView = [[WebViewController alloc]
initWithNibName:#"WebView-iPad" bundle:[NSBundle mainBundle]];
webView.url=#"http://www.passport-america.com/Members/JoinRenew.aspx";
[self.navigationController pushViewController:webView animated:YES];
webView.isUsingiPad = YES;
}else {
webView = [[WebViewController alloc]
initWithNibName:#"WebView" bundle:[NSBundle mainBundle]];
webView.url=#"http://www.passport-america.com/Members/JoinRenew.aspx";
[self.navigationController pushViewController:webView animated:YES];
}
}
-(IBAction) iPadContactUs:(id)sender {
[[UIApplication sharedApplication] openURL:[NSURL URLWithString: #"mailto:info#passport-america.com"]];
}
-(void)animationDidStop:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {
if ([animationID isEqualToString:#"slideMenu"]){
UIView *sq = (__bridge UIView *) context;
[sq removeFromSuperview];
}
}
-(void) positionViews {
if (isUsingiPad) {
UIInterfaceOrientation destOrientation = self.interfaceOrientation;
if (destOrientation == UIInterfaceOrientationPortrait || destOrientation == UIInterfaceOrientationPortraitUpsideDown) {
PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
initWithNibName:#"PassportAmericaViewController-iPad" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView2 animated:YES];
homeView2.isUsingiPad = YES;
homeView2.portrait = YES;
homeView2.landscape = NO;
}else{
PassportAmericaViewController *homeView2 = [[PassportAmericaViewController alloc]
initWithNibName:#"PassportAmericaViewController-iPadLandscape" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController:homeView2 animated:YES];
homeView2.isUsingiPad = YES;
homeView2.portrait = NO;
homeView2.landscape = YES;
}
}
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if (isUsingiPad) {
return YES;
}else{
// Return YES for supported orientations
return NO;
}
}
-(void) willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration: (NSTimeInterval)duration {
if (isUsingiPad) {
[self positionViews];
}else{
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
#end
Edit
It looks like you need to do your work here
BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? #"landscapeNibName" : #"portraitNibName";
RegistrationViewController *regView = [[RegistrationViewController alloc] initWithNibName:nibName bundle:[NSBundle mainBundle]];
I'm not sure where you are getting stuck...
- (id)init;
{
BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? #"landscapeNibName" : #"portraitNibName";
self = [super initWithNibName:nibName bundle:nil];
if (self) {
// any other init stuff
}
return self;
}
or if you prefer to name the nib when you instantiate
BOOL isLandscape = UIInterfaceOrientationIsLandscape([UIApplication sharedApplication].statusBarOrientation);
NSString *nibName = isLandscape ? #"landscapeNibName" : #"portraitNibName";
MyViewController *viewController = [[MyViewController alloc] initWithNibName:nibName bundle:nil];

pushViewController:detail not pushing detailView

I had my app navigating views perfectly. Now, for some reason that I can't figure out, my detailView is not presenting on the pushViewController:detail method in my modal view.
I cannot figure out why it's not working any more. Any help would be appreciated. Thanks.
Here's the method:
- (void)tableView:(UITableView *)tView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row == 0){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
if(indexPath.row == 1){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
if(indexPath.row == 2){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
if(indexPath.row == 3){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
if(indexPath.row == 4){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
}
Here's the class code. I'm not sure if I need to pass in a navigationController with modalView:
//
// ModalView.m
// DiningLog
//
// Created by Eric Rea on 10/10/11.
// Copyright 2011 Avid. All rights reserved.
//
#import "ModalView.h"
#import "DetailView.h"
#implementation ModalView
#synthesize tableView, excersizeName, numSets, time, restTime, dateFormatter, rating, excersizeArray, plistArray, numberWithBool;
-(IBAction) cancel:(id)sender{
[self dismissModalViewControllerAnimated:YES];
}
-(IBAction) save:(id)sender{
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"ExcersizeList.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:#"Excersizes" ofType:#"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property list into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(#"Error reading plist: %#, format: %d", errorDesc, format);
}
// assign values
self.plistArray = [NSMutableArray arrayWithArray:[temp objectForKey:#"Excersizes"]];
NSLog(#"The contents of plistArray is%#", plistArray);
// set the variables to the values in the text fields
self.excersizeArray = [[NSMutableArray alloc] initWithCapacity:4];
[excersizeArray addObject:excersizeName];
[excersizeArray addObject:numSets];
[excersizeArray addObject:time];
[excersizeArray addObject:restTime];
[plistArray addObject:excersizeArray];
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: plistArray, nil] forKeys:[NSArray arrayWithObjects: #"Excersizes", nil]];
NSString *error = nil;
// create NSData from dictionary
// NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistDict)
{
// write plistData to our Data.plist file
[plistDict writeToFile:plistPath atomically:YES];
}
else
{
NSLog(#"Error in saveData: %#", error);
[error release];
}
[self dismissModalViewControllerAnimated:YES];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tView {
// Return the number of sections.
return 1;
}
-(void)setObject:(id)object forNum:(int)num{
if(num == 0){
self.excersizeName = object;
NSLog(#"res %#", self.excersizeName);
}else if(num == 1){
self.numSets = object;
NSLog(#"res %#", self.numSets);
}else if(num == 2){
self.time = object;
NSLog(#"res %#", self.time);
}else if(num == 3){
self.restTime = object;
NSLog(#"res %#", self.restTime);
}else if(num == 4){
self.rating = [object floatValue];
}
[tableView reloadData];
}
- (NSInteger)tableView:(UITableView *)tView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return 5;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier] autorelease];
}
if(indexPath.row == 0){
cell.textLabel.text = #"Excersize";
cell.detailTextLabel.text = excersizeName;
}
if(indexPath.row == 1){
cell.textLabel.text = #"Sets";
cell.detailTextLabel.text = numSets;
}
if(indexPath.row == 2){
cell.textLabel.text = #"Time";
cell.detailTextLabel.text = time;
}
if(indexPath.row == 3){
cell.textLabel.text = #"Rest";
cell.detailTextLabel.text = restTime;
}
if(indexPath.row == 4){
cell.textLabel.text = #"Rating";
cell.detailTextLabel.text = [NSString stringWithFormat:#"%f",rating];
}
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
return cell;
}
- (NSDateFormatter *)dateFormatter {
if (dateFormatter == nil) {
dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterMediumStyle];
[dateFormatter setTimeStyle:NSDateFormatterNoStyle];
}
return dateFormatter;
}
#pragma mark -
#pragma mark Table view delegate
- (void)tableView:(UITableView *)tView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
[tView deselectRowAtIndexPath:indexPath animated:YES];
if(indexPath.row == 0){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
if(indexPath.row == 1){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
if(indexPath.row == 2){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
if(indexPath.row == 3){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
if(indexPath.row == 4){
NSLog(#"hey");
DetailView * detail = [[DetailView alloc] initWithNumber:indexPath.row];
detail.delegate = self;
[[self navigationController] pushViewController:detail animated:YES];
}
}
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
/*
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
*/
/*
// 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 {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[super dealloc];
}
#end
Have you debugged and checked that your navigationController isn't nil ?
I figured it out. I accidentally erased the method insertNewObject in my rootViewController. When I rewrote it, I forgot to add in ModalView * modal = [[ModalView alloc] init];. That was the problem.
Here's what the method looks like now that it's working:
- (void)insertNewObject {
ModalView * modal = [[ModalView alloc] init];
UINavigationController *controller = [[UINavigationController alloc] initWithRootViewController:modal];
[self presentModalViewController:controller animated:YES];
}