CCMenuItem doesn't respond subclassing and adding cctouch methods to CCMenu - iphone

I'm making a menu, and I want one of the buttons to respond when user touch it down, so I made a subclass of CCMenu in order to add cctouchbegan method And manage it there.
The problem is that I can make it to respond both things (menuItem & cctouch), is this normal?
is there a way to force it to do both things?
Thank you in advance, let me know if you need me to put some of the code here

You should look at CCMenu.m -- it already implements ccTouchBegan and sets a selected flag on CCMenuItems. Your approach is probably not working because you're stealing the messages from your parent class.
Your subclass should call [super ccTouchBegan...] first, then check the selected state of the CCMenuItems to determine which button to change visually.
EDIT:
Or, even easier! -- Subclass the appropriate CCMenuItem subclass (e.g. CCMenuItemSprite) and overload the selected method from it's default to include your visual alterations:
-(void) selected
{
[super selected];
//call method to update visuals here
}

You can simple create your own CCLayer subclass, set it's isTouchEnabled property to YES and implement any touch logic you want

Related

Timing for add Notification observer in UIViewController subclass

I'm fairly new to Cocoa Touch. Right now I'm trying to subclass UIViewController to provide my custom view. Since I intend to save the content of a UITextField (passcodeField) using NSUserDefaults, I want to be notified whenever the UITextField changes its value.
I've read somewhere that in order to do that I should add the view controller to be an observer of the UITextFieldTextDidChangeNotification notification. However I'm just not sure when to do that. I've considered several options.
In the -loadView method. However, since I'm loading my view using a XIB, I think i shouldn't mess with this method and should instead leave it as-is. (Am I correct on this point, BTW? )
In the -viewWillAppear method. But this method may be called multiple times because the view may be moved out and into the screen without being destroyed and recreated. (Am I correct? ) This will not do any harm to the program but sure doesn't seem like the correct way.
In the initializer of the UIViewController. If I want to add the notification there I must reference the UITextField. By doing this I essentially cause the view to created before it is really needed. Also I think I read somewhere that if the system runs low on memory the offscreen views may be destroyed. Thus I may lose the notification observing if such thing happens, right?
So I'm totally confused right now. Could you guys give me some advice of where to put it? Thanks so much!
Put it in the - (void)viewDidLoad method of your ViewController remember to call [super viewDidLoad]; at the start of your implementation.

MBProgressHUD tap to cancel, longer text?

I'd like to use MBProgressHUD (or similar look) as alternative to default UIAlertView.
I need a canceling capability on this view.
I tried adding the following method to MBProgressHUD class but it didn't get called when touched.
Any idea?
(void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*)event
I can't use gesture recognizer since my lowest target version is 3.1.2.
Also, it seems complex to enlarge label size for MBProgressHUD's text.
Are there altanatives than fixing MBProgessHUD for the purpose?
I just had quick look at MBProgressHUD and would use that. First, change the size of the HUD by modifying layoutSubviews in MBProgressHUD.h. I would then create a new button class (UIButton subclass) and add this as a subview of the HUD.
This is a super old thread, but it would be way easier just to set hud's UserInteractionEnabled:YES and add a tapGestureRecognizer to it.
Cheers.

General advice on tracking an object's changes

I have an option menu where a user can change the application's settings. I need to track the changes. There is a DONE button, I want to make changes to the data model only when the button is pressed.
My question is, I am not sure the best way to track the changes. I don't want to introduce a giant if blocks in the doneButtonPressed button. Any general advice?
Well, the generic answer is: add callback to your controls in your options screen.
For example if you are using UISlider, then you have to customize it slightly. Probably create a subclass, that would receive touch events and then you redirect them to the delegate. OR you can use this one: RCSwitch
If you are using UIButton's then it's even easier: just add action to it.
After that you can create method like:
-(void) controlDidChange:(UIView*) control {
//mark changed items here
}

Is it possible to allow the uiimageview to be pressed and linked to another uiviewcontroller?

Is it possible to allow the uiimageview to be pressed and linked to another uiviewcontroller?
Yes of course but you will find things much easier if you create an invisible UIButton (of type UIButtonTypeCustom) and place it over the UIImageView. It will process a touchUpInside as simply as a button because... it is a button. You can't see it but it can still capture a touch.
Because a UIImageView is a UIView it inherits methods to handle gestures and multi-touch events. Because it is also a UIResponder it inherits useful methods like touchesBegan and family. So you can use those methods to capture events yourself, but the button is the easiest way and is much easier to do in Interface Builder if you prefer to do it that way.
In the method handling the event, the familiar pattern
PriceViewController* priceVC = [[PriceViewController alloc] init];
[self.navigationController pushViewController:priceVC animated:YES];
[priceVC release];
is the rest of what you need.

How to detect when a UIView has changed size?

I have a UIViewController that is initialised with a correct frame, however somewhere in my code the frame gets mangled and I'm having difficulty finding out where.
In situations like this it is usually handy to watch a variable in the debugger, however I have no way of accessing the controller->view->frame property in my variable view, since it isn't a variable, it's a property (surprisingly enough)
Drilling into the UIView in the variables display shows a few things but nothing I can relate to the frame, I thought perhaps that would be in layer but it isn't.
Is there any way to watch for changes in a private API? I guess not, since the variables are essentially 'hidden' and so you can't specify exactly what to watch.
Alternatively, what other approach could I use? I already tried subclassing UIView, setting my UIViewController's view to point to this subclass and breaking on the setFrame method but it didn't seem to work.
EDIT: the subclassing UIView method DID work, I just had to set the view to point to my test subclass in viewDidLoad and not the init method. Leaving this question open as I'm not sure if this is the best way of approaching this kind of problem...
Subclass your the view you want to track and rewrite the setFrame method:
#implementation MyTableView
- (void)setFrame:(CGRect)frame;
{
NSLog(#"%#", frame);
[super setFrame:frame];
}
#end
Then use the debugger to add a breakpoint to it and check when it gets called. Eventually, you'll see when the frame gets changed and where does the change comes from.
I discovered this can be done using key value observers.
http://developer.apple.com/library/ios/#documentation/cocoa/conceptual/KeyValueObserving/KeyValueObserving.html
You could create an ivar, view2, and just assigned it to your view in your loadView method. That should enable you to watch it like a normal variable.