UIPageControl with UIView with button - iphone

I'm new to Objective-C and need help!
I'm following the example "PageControl" which can be found on the net. I added a button to the view in the NIB and hooked up an action which the implementation is found below.
Here is my definition for the view controller being displayed in the page control:
//ContactCardViewController.h
#interface ContactCardViewController : UIViewController
{
int pageNumber;
NSMutableString *s;
}
//ContactCardViewController.m implementation
- (id)initWithPageNumber:(int)page {
if (self = [super initWithNibName:#"ContactCardViewController" bundle:nil]) {
s = [[NSString alloc] initWithFormat:#"page: %d", page];
}
return self;
}
- (id)initWithString:(NSDictionary *)_s {
if (self = [super initWithNibName:#"ContactCardViewController" bundle:nil]) {
NSMutableString *t = [[NSMutableString alloc] initWithString:_s];
s = t;
}
return self;
}
-(IBAction)callOrAddToContacts:(id)sender
{
jobtitleLabel.text = s;
}
//AppDelegate.m
//in delegate that loads the scroll view with the view for the current page:
- (void)loadScrollViewWithPage:(int)page {
//init the view this way the page: and the correct page number is displayed
controller = [[ContactCardViewController alloc] initWithPageNumber:page ];
//init the view this way, the value of the first person is always displayed
controller = [[ContactCardViewController alloc] initWithString:[[self.allNames objectAtIndex:page] objectForKey:#"firstname"]];
}
Please help me to understand why when the view is created with initWithString and then accessed via the button action only value for the first person in the list is always returned. When i init the view using initWithPageNumber s has the correct value Page: X.

In the InitWithString code, you're passing in a Dictionary, not a string.
- (id)initWithString:(NSDictionary *)_s {
if (self = [super initWithNibName:#"ContactCardViewController" bundle:nil]) {
NSMutableString *t = [[NSMutableString alloc] initWithString:_s];
s = t;
}
return self;
}
You may want to change that to NSString, or change the NSMutableString... to an instance of NSDictionary. One or the other.

Found out the problem was in the plist file. The code is fine.

Related

Trouble with datasource not being called from viewWillAppear

A little background. I have taken GCCalendar which only works in portrait orientation and extended it to work in landscape similar to how the iPhone's Calendar app works. I did this by duplicating the main view class and modifying it to work in landscape. So when the phone is in portrait orientation the CGCalendar is instantiated from the apps main view controller using the original portrait view class and when in landscape orientation using the new modified landscape view class. Most of the other classes in GCCalendar are shared without modification. A few had to be duplicated as well.
I got it all working great except for an issue with the datasource. The datasource is called when the calendar is first loaded and each time the user changes the dates being viewed. Problem is I can't get the datasource call to work on the first call.
I am stumped as it works fine in portrait orientation and I cannot find any difference between the 2 versions.
Following is some of the code that shows how it gets to the datasource call the first time. Subsequent calls removes all the calendar subviews and instantiates them again with the new dates. The duplicated landscape class names end in LS. Otherwise as you can see they are identical.
Does anyone has any idea of where else I might look to resolve this issue?
Thanks,
John
--------------------------------
//App main view controller
- (void)showLandscapeCalendar {
GCCalendarLandscapeView *calendar = [[[GCCalendarLandscapeView alloc] init] autorelease];
calendar.dataSource = self;
calendar.delegate = self;
navigationController = [[UINavigationController alloc] initWithRootViewController:calendar];
[self presentModalViewController:navigationController animated:YES];
}
- (void)showPortraitCalendar {
GCCalendarPortraitView *calendar = [[[GCCalendarPortraitView alloc] init] autorelease];
calendar.dataSource = self;
calendar.delegate = self;
navigationController = [[UINavigationController alloc] initWithRootViewController:calendar];
[self presentModalViewController:navigationController animated:YES];
}
- (NSArray *)calendarEventsForDate:(NSDate *)date{
//build and return the events array
//this is the protocol datasource method
//It is supposed to run every time the date changes in the calendar
}
-------------------------------
// GCCalendarLandscapeView...
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (viewDirty) {
[self reloadDayAnimated:NO context:NULL];
viewDirty = NO;
}
viewVisible = YES;
}
- (void)reloadDayAnimated:(BOOL)animated context:(void *)context {
GCCalendarDayViewLS *nextDayView = [[GCCalendarDayViewLS alloc] initWithCalendarView:self];
}
-------------------------------
//GCCalendarDayViewLS
- (id)initWithCalendarView:(GCCalendarView *)view {
if (self = [super init]) {
dataSource = view.dataSource;
}
return self;
}
- (void)reloadData {
//** first time through the dataSource method does not run
events = [dataSource calendarEventsForDate:date];
}
-------------------------------
// GCCalendarPortraitView...
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
if (viewDirty) {
[self reloadDayAnimated:NO context:NULL];
viewDirty = NO;
}
viewVisible = YES;
}
- (void)reloadDayAnimated:(BOOL)animated context:(void *)context {
GCCalendarDayView *nextDayView = [[GCCalendarDayView alloc] initWithCalendarView:self];
}
-------------------------------
//GCCalendarDayView
- (id)initWithCalendarView:(GCCalendarView *)view {
if (self = [super init]) {
dataSource = view.dataSource;
}
return self;
}
- (void)reloadData {
**//this one works every time
events = [dataSource calendarEventsForDate:date];
}

App Crash on setting UITableViewCell:textLabel:text

I am creating a split-view iPad application. When the user presses the bar button item in the master view, a modal is presented. This modal has a textfield and has an IBAction to pick up keyboard returns.
On keyboard returns, a new instance of my Farm class is created (code below). This instance is then added to an array that is stored in my delegate. I then try to reload the MasterViewController's table. Upon this reload the application crashes on cell.textLabel.text with a EXC_BAD_ACCESS error.
Farm *current = [delegate.arrayOfFarms objectAtIndex:indexPath.row];
cell.textLabel.text = [current getFarmTitle];
If I ask the array within the delegate how many elements it has, it will indeed show the current amount, even. This is what is bizarre to me about this whole thing: the Farm instances appear to be in existence.
I have instances of AppDelegate in both my MasterViewController and my NewFarmNamingView classes. The instance in the Master is to populate the table. The instance in NewFarm is to add the newly created Farm to the delegate. Code below.
Segments from class NewFarmNamingView:
- (IBAction) keyboardDonePushed:(id)sender
{
// create a Farm and add it to the delegate
NSString *text = newFarmTextField.text;
Farm *newFarm = [[Farm alloc] init];
[newFarm setFarmTitle:text];
[[delegate arrayOfFarms] addObject:newFarm];
[newFarm release];
NSLog(#"Added farm: %#" , text);
// dismiss the view
[self closeView:nil];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// initialize the delegate
delegate = [[UIApplication sharedApplication] delegate];
}
Segments from the class Farm
- (void) setFarmTitle : (NSString *) _farmTitle
{
farmTitle = _farmTitle;
}
- (NSString *) getFarmTitle
{
return farmTitle;
}
// NSCoding Methods
- (void) encodeWithCoder:(NSCoder *)aCoder
{
[aCoder encodeObject:farmTitle forKey:#"kFarmTitle"];
}
- (id) initWithCoder:(NSCoder *)aDecoder
{
farmTitle = [aDecoder decodeObjectForKey:#"kFarmTitle"];
return self;
}
// Initialization method
- (id)init
{
self = [super init];
if (self) {
// Initialization code here.
}
return self;
}
From the runtime reference: "objc_msgsend sends a message to the receiver and expects a simple return value."
I'll bet you anything that what you're returning (if you're returning anything at all) in that class method getTitleFarm is returning an incorrect value. It should be an NSString. Be absolutely sure it is returning an NSString, and not anything else.
If you need to use the respondsToSelector method to see if the class is being released, try:
if([current respondsToSelector:#selector(getFarmTitles)]) {. [current getFarmTitle];
}
else {
NSLog:(#"FAILURE!!");
}
EDIT: maybe you are not retaining or even creating this string at all. In it's initialization, wrap it in a retain]; message

Same UIViewController being loaded from 2 different sources but with different results

In my application I have 2 separate ways to get to the same UIViewController, on way is to go through 2 parent ViewControllers created using the storyboard and this way works perfectly just passing and id through each segue transition.
The other way (Which is what I need help with) goes directly from a button pressed on the RootViewController, it also just passes the id to a customly defined object called Event. The code for each transition is below:
From the segue:
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
NSIndexPath *path = [self.tableView indexPathForSelectedRow];
Event *event = [nEventList objectAtIndex:path.row];
EventInfo *eventInfo = segue.destinationViewController; //Create new ViewController
eventInfo.eventID = event.id;
}
From the button:
- (void)FeaturedPressedAction:(id)sender
{
UIButton *button = (UIButton *)sender;
int tag = button.tag;
EventInfo *eventInfo = [[EventInfo alloc] init]; //Create new ViewController
eventInfo.eventID = #"1"; //There is an event with id '1'
[self.navigationController pushViewController:eventInfo animated:YES];
}
And the code to set up the ViewController:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIImage *image = [UIImage imageNamed:#"title.png"];
self.navigationItem.titleView = [[UIImageView alloc] initWithImage:image];
SingletonClass* myapp = [SingletonClass sharedInstance];
events = [myapp getEvents];
// Loop to filter out the events that Do not have this ID
Event *event;
BOOL found = false;
for (int i = 0; i < [events count]; i++) {
if(!found){
event = [events objectAtIndex:i];
if([event.id isEqualToString: eventID]){
found = true;
NSLog(#"Found the event with title %#", event.title);
}
}
}
self.eventTitle.text = event.title;
self.eventLocation.text = event.location;
self.eventDate.text = event.date;
self.eventTime.text = #"22:00";
self.eventDescription.text = event.details;
}
It finds the event fine as I can see with the NSLog's left in there, just none of the view loads when coming from the button press. Where am I going wrong?
Rather than alloc your EventInfo*, you should instantiate it from your storyboard instead.
So in your FeaturedPressedAction replace the initialisation line with:
EventInfo *eventInfo = [self.storyboard instantiateViewControllerWithIdentifier:#"..."];
And make sure you identify the ViewController in Interface Builder with the same identifier. Replace ... with something meaningful.

AQGridview Not Calling Datasource Methods

I am trying to implement AQGridView based upon the ImageDemo in the /examples folder. I have a view controller with the following declaration:
#interface ImageDemoViewController : UIViewController <AQGridViewDelegate, AQGridViewDataSource, ImageDemoCellChooserDelegate>
{
...
None of the datasource methods in my view controller such as
- (NSUInteger) numberOfItemsInGridView: (AQGridView *) aGridView
{
return ( [images count] );
}
are being called. Here is where I setup the gridview making my view controller the delegate for the gridview.
- (void)viewDidLoad
{
[super viewDidLoad];
self.gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.gridView.autoresizesSubviews = YES;
self.gridView.delegate = self;
self.gridView.dataSource = self;
images=[[NSMutableArray alloc]init];
[images addObject:#"http://t3.gstatic.com/images?q=tbn:ANd9GcTOXAzFMoK441mcn9V0OemVe_dtAuCpGjBkLrv4rffyOjYIo45BEw"];
[self.gridView reloadData];
}
If I set a breakpoint on
[self.gridView reloadData];
the line is executed but reloadData method in AQGridView is not called. The only difference from the ImageDemo is I do not have a .xib file for the view controller. Have I forgotten to hook up something, resulting in the datasource methods not being called?
If there's no XIB, then who's creating the gridView? If it's never created, then it would be NIL, and you'd have the behavior you describe. (If that's it, then just adding:
self.gridview = [AQGridView alloc] initWithFrame: ...]; should suffice.
Had the same problem. Solved by replacing the view with the AQGridView.
[self.view addSubview:self.gridView]
self.view = self.gridView;
Full method:
- (void) viewDidLoad
{
[super viewDidLoad];
self.gridView = [[AQGridView alloc] init];
self.gridView.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;
self.gridView.autoresizesSubviews = YES;
self.gridView.delegate = self;
self.gridView.dataSource = self;
self.view = self.gridView;
[self.gridView reloadData];
}
Maybe you could try implementing this:
- (void)LoadSearch
{
NSURL *test1 = [NSURL URLWithString:#"http://www.4ddraws.com/search_iphone.asp"];
NSURLRequest *test = [NSURLRequest requestWithURL:test1];
[web4D setScalesPageToFit:(YES)];
[web4D loadRequest:test];
}

Initializing view controller referenced from application launch

My data is stored in a member variable (NSArray) of a view controller. The problem I'm running into is that my data is loaded from a database on application launch, but the NSArray isn't initialized until later, so the addObject calls silently fail.
I've tried putting breakpoints on the init, initWithNibName, viewWillAppear, and viewDidLoad methods of my view controller (SafeTableViewController), but none of them catch before the addObject call. I assume that the actual view controller is initialized, because when I watch it in the debugger it has a nonzero address, but the NSArray has the address 0x0 when addObject is called.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
databaseName = #"DubbleDatabase.sql";
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDir = [documentPaths objectAtIndex:0];
databasePath = [documentsDir stringByAppendingPathComponent:databaseName];
[self checkAndCreateDatabase];
[self readSafeItemsFromDatabase ];
// Add the tab bar controller's current view as a subview of the window
[window addSubview:tabBarController.view];
[window makeKeyAndVisible];
return YES;
}
- (void) readSafeItemsFromDatabase {
// some code skipped here, but basically: open sqlite3 database, iterate through rows
while(sqlite3_step(compiledStatement) == SQLITE_ROW) {
// read database, get data fields out
SafeItem *safeItem = [[SafeItem alloc] initWithName:aName price:aPrice category:aCategory];
[safeTableViewController addItemToSafe: safeItem]; // PROBLEM HERE
[safeItem release];
}
}
sqlite3_close(database);
}
In SafeTableViewController.m:
- (void) addItemToSafe : (SafeItem*) newSafeItem {
[self.safeItems addObject: newSafeItem];
}
// I put a breakpoint on this, but it does not hit. i.e. safeItems is not initialized when addObject is called on it.
-(id) init {
if(self = [super initWithNibName:#"SafeTableViewController" bundle:nil]){
self.safeItems = [[NSMutableArray alloc] init];
}
return self;
}
EDIT: Thought of a way to fix this problem. Still curious though: when is init and/or initWithNibName called? Here's the proposed solution:
- (void) addItemToSafe : (SafeItem*) newSafeItem {
if(self.safeItems == nil){
self.safeItems = [[NSMutableArray alloc] init];
}
[self.safeItems addObject: newSafeItem];
}
How do you setup your instance of SafeTableViewController? By code? By nib?
-(id) init is not the designated initializer.
You probably want to use
- (id)initWithNibName:(NSString *)nibName bundle:(NSBundle *)nibBundle {
if(self = [super initWithNibName:nibName bundle:nibBundle]){
self.safeItems = [[NSMutableArray alloc] init];
}
return self;
}
or initialize elsewhere, i.e. in viewDidLoad.
The problem is that you shouldn't be storing your data in a view controller. Create a model object (SafeItemManager for instance) to hold your data and point the view controller at that.