ios: Done and Cancel do not work on the second app tutorial at apple tut site - ios4

Version 4.3.2 of XCODE.
This is causing me no end of headaches trying to figure out how to do the tutorial. I have checked the code against their code and it looks right. I downloaded someones else's copy and it works but with my copy done and cancel do not work.
AddSightingViewController.m code looks like this
#import "AddSightingViewController.h"
#implementation AddSightingViewController
...
- (IBAction)cancel:(id)sender {
[[self delegate] addSightingViewControllerDidCancel:self];
}
- (IBAction)done:(id)sender {
[[self delegate] addSightingViewControllerDidFinish:self name:self.birdNameInput.text location:self.locationInput.text];
}
...
And debug checkpoints on these lines work correctly
However in BirdsMasterViewController.m I have the following code:
#import "BirdsMasterViewController.h"
#import "BirdsDetailViewController.h"
#import "BirdSightingDataController.h"
#import "BirdSighting.h"
#import "AddSightingViewController.h"
#interface BirdsMasterViewController () <AddSightingViewControllerDelegate>
#end
#implementation BirdsMasterViewController
#synthesize dataController = _dataController;
...
- (void)addSightingViewControllerDidCancel:(AddSightingViewController *)controller {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)addSightingViewControllerDidFinish:(AddSightingViewController *)controller name:(NSString *)name location:(NSString *)location {
if ([name length] || [location length]) {
[self.dataController addBirdSightingWithName:name location:location];
[[self tableView] reloadData];
}
[self dismissModalViewControllerAnimated:YES];
}
...
and debug checkpoints on these lines never stop the program on my code (but do on the downloaded code).
What in the heck am I missing to get this to work.

Related

Subview not closing causing Memory Leak Phonegap

I am having issues not being able to close CDVViewController when calling this custom refresh, it stays loaded in the background and loads a new one causing memory leaks/high memory usage. I have tried to use both release and removeFromSuperview for the webView, subview, CDVViewController, ChildViewController but none of them work. I am running a Tab Bar Controller via Storyboard, (another reason why I have to call CDVViewController as a subview) and when I use the web inspector via safari I can see the pages building up on that tab, and when I refresh on another tab (not using CDVViewController/phonegap) it works fine. Any help is much appreciated.
So here is my .h
#import <UIKit/UIKit.h>
#import <ifaddrs.h>
#import <arpa/inet.h>
#import <Cordova/CDVViewController.h>
#interface ThirdViewController : CDVViewController <UIWebViewDelegate>
{
IBOutlet CDVViewController *webView;
IBOutlet UIActivityIndicatorView *activityind;
}
- (IBAction)Refresh:(id)sender;
#end
And my .m
#interface ThirdViewController ()
#end
#implementation ThirdViewController
- (void)viewDidLoad
{
webView = [CDVViewController new];
webView.startPage = #"MYWEBSITE";
[self addChildViewController:webView];
[self.webView addSubview:webView.view];
webView.view.frame = CGRectMake(0, 0, 0, 0);
[activityind startAnimating];
[self.webView addSubview: activityind];
[self CustomActivity];
}
-(void) CustomActivity
{
if ([webView.reference isEqualToString:#"TRUE"]){ //webView.reference is something I added into phonegap CDVViewController so that I am able to see when it is being loaded/used to load the custom activity/activity ind to work.
[activityind removeFromSuperview];
}else{
[NSTimer scheduledTimerWithTimeInterval:.5 target:self selector:#selector(CustomActivity) userInfo:nil repeats:NO];
}
}
- (IBAction)Refresh:(id)sender {
webView = [CDVViewController new];
webView.startPage = #"MYWEBSITE";
[self addChildViewController:webView];
[self.webView addSubview:webView.view];
webView.view.frame = CGRectMake (0,0,self.view.frame.size.width,(self.view.frame.size.height -44));
[activityind startAnimating];
[self.webView addSubview: activityind];
[self CustomActivity];
}
- (void)webViewDidStartLoad:(UIWebView *)webView {}
- (void)webViewDidFinishLoad:(UIWebView *)subview {}
- (void)loading {}
- (void)didReceiveMemoryWarning{[super didReceiveMemoryWarning];}
#end
Found the solution, I had to implement [webView.webView reload];
- (IBAction)Refresh:(id)sender {
[webView.webView reload];
}

Updating the UI control's value in the runtime from background thread.

I know this sound a very simple question. I'm still a newbie in iPad app development. Would you please tell me how would I update the contents in the run time. The following code may be helpful to know my question in detail.
In the following code, I'm trying to display text contents in the TexViewcontrol. When I change the for loop form 10 to 100000, I needed to wait until everything fills up. I need to display the contents on the screen while loop is running.
- (IBAction)RunHelloWorld:(id)sender
{
tctView.text = #"tt";
for(int i=0;i<10;i++)
{
NSString *result1 = #"Testing";
tctView.text = [tctView.text stringByAppendingString:result1];
tctView.scrollEnabled = YES;
}
}
Would you please give me any help? Thanks a LOT!!!
-Teva
Finally I found the solution with the following URL's help. http://samplecodebank.blogspot.com/2011/05/nsthread-example_12.html
Thanks a LOT!
This is my code.
PCViewController.h file code
#import <UIKit/UIKit.h>
#interface PCViewController : UIViewController
{
IBOutlet UITextView *txtView;
}
-(void)ReadFile;
- (void) mainThreadSetText:(NSString*)text;
#end
PCViewController.m file code
#import "PCViewController.h"
#interface PCViewController ()
#end
#implementation PCViewController
- (void)viewDidLoad
{
[super viewDidLoad];
[NSThread detachNewThreadSelector:#selector(ReadFile) toTarget:self withObject:nil];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)ReadFile
{
// Check whether current thread is externally terminated or not.
char line[1024];
FILE *fp = fopen("/Users/Teva/Desktop/File1.txt","r");
while([[NSThread currentThread] isCancelled] == NO)
{
if( fp != NULL )
{
while( fgets(line,1024,fp) )
{
printf("%s\n",line);
NSString *result1 = [NSString stringWithCString:line encoding:NSASCIIStringEncoding];
[self performSelectorOnMainThread:#selector(mainThreadSetText:) withObject:result1 waitUntilDone:YES];
[NSThread sleepForTimeInterval:1.0];
}
}
}
}
// Screen processing should be done outside the Thread.
- (void) mainThreadSetText:(NSString*)text
{
txtView.text = [txtView.text stringByAppendingString:text];
txtView.scrollEnabled = YES;
}
- (void)dealloc
{
[txtView release];
[super dealloc];
}
#end

Objective C Theos UIView not working

Well i can't get this too work. My goal is too add a UIButton into Minecraft Pocket Edition. I think the problem is easy or obvious, so please don't get destracted by the small wall of code.
Thanks soo much,
Kivifreak
#import <substrate.h>
#import <UIKit/UIKit.h>
#interface Tpschanger
UIButton *myButton;
UIView *myView;
-(void) TPSButton;
#end
%hook minecraftpeViewController
-(void)viewDidLoad {
myView=[[UIView alloc]initWithFrame:CGRectMake(0,0,320,480)];
myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake(20, 20, 200, 44);
[myButton setTitle:#"TPS" forState:UIControlStateNormal];
[myButton addTarget:self action:#selector(TPSButton:) forControlEvents:UIControlStateNormal];
[self.myView addSubview:myButton];
> I get an error at the previous line: request for member ‘myView’ in ‘self’, which is of non-class type ‘minecraftpeViewController*’
%orig;
}
%new
-(void) TPSButton {
}
%end
It should be [self.view addSubview:myButton]; IMO you don't need myView.

How do I access properties of an object when inside another custom object in Xcode / Obj C?

As I newbie I sort of understand how to create and access properties for an object with dot notation.. eg I have an object golfCourse with a property courseName so golfCourse.courseName=#"My Course" works OK. My problem is that if I create a "holeObject" containing Par and Distance and create Hole1, Hole2 etc inside my Course Object trying to access them by golfCourse.Hole1.Par is returning null... My various searches have not found me any clarity.. Can someone point me in the right direction?
Many thanks
HoleObject.h
#import <Foundation/Foundation.h>
#interface HoleObject : NSObject
#property(nonatomic,strong) NSString* par;
#property(nonatomic,weak) NSString* yellowDistance;
#end
HoleObject.m
#import "HoleObject.h"
#implementation HoleObject
#synthesize
par=_par,
yellowDistance=_yellowDistance,
-(void) encodeWithCoder:(NSCoder*)encoder
{
[encoder encodeObject:self.par forKey:#"Par"];
[encoder encodeObject:self.yellowDistance forKey:#"YellowDistance"];
}
-(id) initWithCoder:(NSCoder*)decoder
{
if((self=[super init]))
{
self.par=[decoder decodeObjectForKey:#"Par"];
self.yellowDistance=[decoder decodeObjectForKey:#"YellowDistance"];
}
return self;
}
#end
CourseObject.h
#import <Foundation/Foundation.h>
#import "HoleObject.h"
#interface CourseObject : NSObject
#property(nonatomic,strong)NSString* courseName;
#property(nonatomic,strong)NSString* courseAddress;
#property(nonatomic,strong)HoleObject* hole1;
#property(nonatomic,weak)HoleObject* hole2;
etc...
#end
CourseObject.m
#import "CourseObject.h"
#implementation CourseObject
#synthesize
courseName=_courseName,
courseAddress=_courseAddress,
hole1=_hole1,
etc;
-(void) encodeWithCoder:(NSCoder*)encoder
{
[encoder encodeObject:self.courseName forKey:#"CourseName"];
[encoder encodeObject:self.courseName forKey:#"CourseAddress"];
[encoder encodeObject:self.hole1 forKey:#"Hole1"];
etc;
}
-(id) initWithCoder:(NSCoder*)decoder
{
if((self = [super init]))
{
self.courseName=[decoder decodeObjectForKey:#"CourseName"];
self.courseAddress=[decoder decodeObjectForKey:#"CourseAddress"];
self.hole1 = [decoder decodeObjectForKey:#"Hole1"];
self.hole2 = [decoder decodeObjectForKey:#"Hole2"];
etc;
}
return self;
}
#end
Then in my main View Controller..
- (void)viewDidLoad
{
[super viewDidLoad];
allCourses=[[NSMutableArray alloc] initWithCapacity:2];
CourseObject*thisCourse;
thisCourse=[[CourseObject alloc]init];
thisCourse.courseName=#"Branston";
thisCourse.courseAddress=#"The World";
thisCourse.hole1.par=#"4";
thisCourse.hole1.yellowDistance=#"336";
but if I then try and NSLog thisCourse.hole1.par Im getting null?
Thanks for looking.
Spent some time trying different things with this as it was pretty clear that the Null was showing the object was not being created properly.. Found that [[CourseObject alloc]init] does not actually do anything for the objects "sub-Objects" and I needed to do course.hole1=[[HoleObject alloc] init] for all 18 holes as well... bit fiddly but seems to work..

UIAlertView showing up very slowly

I just wrote a iOS app to test the UIAlertView. When I ran it, the UIAlertView just appear with the screen went dark first without the UIAlertView, and "a long time" later ,the UIAlertView appeared. This happens on both Simulator and iPhone (IOS 4.2). I don't know why, Please help me, thanks.
Description:(You can also download the Project HERE)
Its a very simple View based app With 3 Classes: AppDelgate ViewControler and a TestOperation which implement NSOperation;
AppDelegate was just the one produced by XCode;
TestOperation.h:
#import <Foundation/Foundation.h>
#protocol TestOperationDelegate
- (void)didFinishTestOperaion;
#end
#interface TestOperation : NSOperation {
id <TestOperationDelegate> delegate;
}
#property (nonatomic, assign) id <TestOperationDelegate> delegate;
#end
TestOperation.m
#import "TestOperation.h"
#implementation TestOperation
#synthesize delegate;
- (void)main {
[delegate didFinishTestOperaion];
}
#end
ViewContoller.m
- (void)viewDidLoad {
[super viewDidLoad];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
TestOperation *testOperation = [[TestOperation alloc] init];
testOperation.delegate = self;
[queue addOperation:testOperation];
[testOperation release];
}
- (void)didFinishTestOperaion {
NSLog(#"start uialertview");
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:#"Oops!" message:#"Here's the UIAlertView"
delegate:self cancelButtonTitle:#"Confirm" otherButtonTitles:nil];
[alert show];
[alert release];
}
//Solved!! Use performSelectorOnMainThread to make
the UI run on Main Thread
Solved!! Use performSelectorOnMainThread to make the UI run on Main Thread
[alert performSelectorOnMainThread:#selector(show) withObject:nil waitUntilDone:NO];
What are you attempting to test in the UIAlertView? If you simply called the UIAlertView from viewDidAppear: in your ViewController, is the UIAlertView displayed rapidly as expected?
I expect that the issues you are having are related to how you are calling the UIAlertView, and that the UIAlertView is being displayed before the UIView controlled by your ViewController has appeared.

Resources