Trying to use KVC in Xcode to update a TextLabel - xcode

I've got a TextField that I'm trying to update the values as it progresses through running my code using KVC. Unfortunately I cannot seem to get anything to update past the initial value.
I've used the bindings on the button that launches the code, the TextField that I want to update and it just doesn't want to update. Please forgive me for the n00bish question but I've been searching online all day, going through tutorials, rewriting the code different ways and can't seem to figure out why this very simple tasks won't work.
Here is my KVC.h file:
#import <Foundation/Foundation.h>
#interface KVC : NSObject{
NSString *_progressString;
}
#property (nonatomic, retain) NSString *progressString;
#end
Here is my App header file:
#import <Cocoa/Cocoa.h>
#import "KVC.h"
//UI Controls
#interface AppDelegate : NSObject <NSApplicationDelegate>
{
NSWindow *window;
NSPersistentStoreCoordinator *__persistentStoreCoordinator;
NSManagedObjectModel *__managedObjectModel;
NSManagedObjectContext *__managedObjectContext;
NSButton *_loadingExtracts;
NSButton *_processStuff;
NSProgressIndicator *_progressBar;
KVC *myProgressString;
}
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, retain, readonly) NSPersistentStoreCoordinator *persistentStoreCoordinator;
#property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel;
#property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext;
#property (assign) IBOutlet NSButton *loadingExtracts;
#property (assign) IBOutlet NSButton *processStuff;
#property (assign) IBOutlet NSProgressIndicator *progressBar;
- (IBAction)saveAction:(id)sender;
- (IBAction)loadingExtracts:(id)sender;
- (IBAction)processStuff:(id)sender;
#end
And finally, here is the function inside the code that I cannot get to update.
- (IBAction)processStuff:(id)sender
{
KVC *frickenHeck = [[KVC alloc] init];
NSLog(#"Button Pressed - Processing Information");
[myProgressString setValue:#"Testing" forKey:#"_progressString"];
[_progressBar setUsesThreadedAnimation:YES];
[_progressBar startAnimation:self];
//Turn off Progress Bar
[_progressBar stopAnimation:self];
[frickenHeck setValue:#"Completed" forKey:#"_progressString"];
//[_progressText setStringValue:#""];
}
(As you can see, I've tried updating 2 different ways and neither work. The allocation seems to set up the initial variable just fine, the Log shows I'm in the method, just can't get my label to update past the allocation).
Any thoughts or ideas would be greatly appreciated.

I am more of a iOS developer, so I am not 100% certain how some of the standards cross-over to the OSX side of things. Nevertheless,
You are saying that you are using KVC, like it is a framework providing a easy to use key-value coding scheme, which doesn't necessarily make sence. This appears to be a custom class you created called KVC.
In your KVC.m file did you #synthesize this variable?
#implementation KVC
#synthesize progressString = _progressString;
You have defined your object frickenHeck as class KVC which holds a property called progressString, that you are making available to other classes via this call.
#property (nonatomic, retain) NSString *progressString;
Assuming you have synthesized the variable in your #implementation file, why don't you just call:
frickenHeck.progressString = #"Testing";
or
frickenHeck.progressString = #"Completed";
Sure you can set the variable via Key Value Coding, but not by setting to the private form of your class variable. Try:
[frickenHeck setValue:#"Completed" forKey:#"progressString"];
So, have you synthesized this variable? Or at least set a accessor setter/getter for the variable? You typically only want to do one or the other.
Header:
- (void)progressString;
- (NSString *)setProgressString:(NSString *)_string;
Implementation:
- (void)progressString {
return _progressString;
}
- (NSString *)setProgressString:(NSString *)_string {
_progressString=_string;
}
Also to note that if you are going to be changing this variable a lot, you may want to use the NSMutableString form of the class, and set the #property declaration to copy. #property (nonatomic, copy) NSMutableString *progressString;
I hope some of this information assists you on your journey..
Mark

Related

why no field "event" and "arguments" at creation action in interface builder?

my file AppDelegate.h
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSViewController <NSApplicationDelegate,NSTableViewDataSource,NSURLConnectionDataDelegate>{
NSMutableArray *users;
NSMutableArray *messages;
NSMutableData *responseData;
NSString *getUsersRequest;
NSString *getMessagesRequest;
int requestStatus;
NSInteger userId;
}
#property (weak) IBOutlet NSTableColumn *tableColumn;
#property (weak) IBOutlet NSTableView *tableUsers;
#property (weak) IBOutlet NSButton *send;
...
I'm trying to create action in xcode:
enter image description here
but I do not see fields "event" and "arguments", Why???
To actually create an action, you should use the .m (implementation) file, not your .h (header) file.
Also, unlike iOS, when developing for OS X and AppKit the options Event and Arguments are not available, this is normal.

Trouble saving segmented control with core data

I thought this issue would be a simple one to over come, but it's providing to be a little harder and frustrating.
I'm using UISegmentedControl to make a selection and then saved to core data with a NSNumber entity. Unfortunately when I try to convert the integer from the segment control into a NSNumber, it comes up (null).
Below is a sample code of my problem.
// ViewController.h
#import <UIKit/UIKit.h>
#import "Object.h"
#interface ViewController : UIViewController
#property (weak, nonatomic) IBOutlet UISegmentedControl *segmentOutlet;
#property (strong, nonatomic) Object *currentObject; //core data object with an entity named subObject as an NSNumber
- (IBAction)segmentControlSelected:(id)sender;
#end
//ViewController.m
-(IBAction)segmentControllerSelected
{
[self.segmentOutlet selectedSegmentIndex] // segment control
currentObject.subObject = [NSNumber numberWithInt:([self.segmentOutlet selectSegmentIndex]);
NSLog(#"the another Object number is %#", currentObject.subObject);
}
The NSLog %# result is (null), so I'm not sure how the correct this.
I've only started to learn programming, so it's probably a basic rookie mistake.
Thanks for any help
Your segmentOutlet needs to be specified as strong. The memory system is discarding it at some point; then, when you try self.segmentOutlet the return value is nil. After than invoking selectSegmentIndex fails and, by default, returns nil and you eventually get to 'null'. Try:
#property (strong, nonatomic) IBOutlet UISegmentedControl *segmentOutlet;

MVC iOS breakout game design

I have the following models in my breakout like game:
Ball.h
Block.m
GamePlay.m
Paddle.h
Paddle.m
I also have the following views:
BallView
BlockVIew
PaddleView
My question is, when I change a property in the model, I have to change the property in the view also. For example:
Ball Model:
#interface Ball : NSObject
#property (nonatomic, strong) NSNumber *radius;
#property (nonatomic, strong) NSNumber *xSpeed;
#property (nonatomic, strong) NSNumber *ySpeed;
#property (nonatomic, strong) NSNumber *xDirection;
#property (nonatomic, strong) NSNumber *yDirection;
#property (nonatomic) CGPoint location;
#property (nonatomic, strong) UIColor *color;
#end
Ball View:
#interface BallView : UIView
#property (strong, nonatomic) UIColor *color;
#property (nonatomic) int radius;
#end
When the model has its properties changed, I have such as location, or color. I have to update these same properties in the view. Should I NOT track location and color in my model?
I think the main point in MVC is maintainability over time. If you decide you need an entirely different view of your game tomorrow, the model code can stay the same, so you can focus on modifying just the view part.
Also, if you know you need some data about the game's state, you know where you can find it - in the model. For example, you might want to transmit the ball's location over the internet to your friend, if he is interested in watching you breaking blocks.
As you can probably guess from the last two paragraphs, you should indeed store and update the location and color of the ball in the model. The view shouldn't be the owner of app state.

Updating a text field from the URL from WebKit

First off let me say that I am VERY new to COCOA but I am learning. I have a simple web browser built. It loads a default page, takes URLs from a text field, even has google search. The problem I'm running in to is updating the textfeild with the current URL. I have the code as follows...
In the .h is...
#import <Cocoa/Cocoa.h>
#import <WebKit/WebKit.h>
#interface MustangAppDelegate : NSObject <NSApplicationDelegate> {
NSWindow *window;
NSTextField *googleSearch;
WebView *webView;
NSTextField *addressBar;
}
#property (assign) IBOutlet NSWindow *window;
#property (nonatomic, retain) IBOutlet NSTextField *googleSearch;
#property (nonatomic, retain) IBOutlet WebView *webView;
#property (nonatomic, retain) IBOutlet NSTextField *addressBar;
- (IBAction)googleSearch:(id)sender;
- (IBAction)homeAction:(id)sender;
- (IBAction)addressBar:(id)sender;
#end
In the .m I have a bunch so I'll put in what I think is relevant here...
#import "MustangAppDelegate.h"
#implementation MustangAppDelegate
#synthesize window;
#synthesize googleSearch;
#synthesize webView;
#synthesize addressBar;
- (void)webView:(WebView *)sender didStartProvisionalLoadForFrame:(WebFrame *)frame {
NSURLRequest *currentRequest = [webView request];
NSURL *currentURL = [currentRequest URL];
addressBar.stringValue = currentURL.absoluteString;
}
The code for the above method never get's called. Like I said, I'm very new to OBC & Cocoa but have been programming in things like Perl for many years. Seems like I'm missing some connectivity because I get a warning with the above code that states WebView may not respond to the request. I dug around to find that code but it still seems like I'm missing something...
Thanks
Mike
Just CTRL + drag the -frameLoadDelegate from the IB to the App Delegate of your application, Mustang
use -didFinishLoadForFrame for the URL
and if you need the title use, -didRecieveTitle.
Hope it helped !

objective C: may not respond to (issue)

I have two UINavigationController in the appdelegate.h
{
UINavigationController *leftView;
UINavigationController *rightView;
UIWindow *window;
}
#property (nonatomic, retain) IBOutlet UIWindow *window;
#property (nonatomic, retain) IBOutlet UINavigationController *leftView;
#property (nonatomic, retain) IBOutlet UINavigationController *rightView;
appdelegate.m
#synthesize leftView;
#synthesize rightView;
then in a different class
test.m
#import "appdelegate.h"
if I do:
[self leftView] pushViewController...; //(Everything is ok)
but if I change it to:
[self rightView] pushViewControll...; //it complains about ViewController may not respond to -rightView
You code seems right; what doesn't seem right is that you're including AppDelegate. If the code was in AppDelegate.m, it would probably work without any complaints by the compiler.
Instead, you've defined #property lines for each, but that is in AppDelegate.h, not test.h - but you're using them in test.m. That is probably the source of the problem.
Interestingly, does the code actually run?
I'm going to hazard a guess: Have you declared and synthesized a property for rightView, or at least provided your own manual getter?
It sounds like you declared a #property or method called leftView that returns the leftView ivar, but you forgot to do the same for rightView. The error is telling you that self doesn't respond to the selector rightView.

Resources