make an imageView move in objective-c - objective-c

I'm trying to make a bird jump by pressing a button. But i have not idea how to do that. I created a timer but i don't know how to use that.
int birdFlight;
#interface BirdViewController : UIViewController
{
IBOutlet UIButton *flyButton;
IBOutlet UIImageView *bird;
NSTimer *birdMovement;
}
-(IBAction)buttonBirdFly:(id)sender;
#end
How to use this method?
#implementation BirdViewController
-(IBAction)buttonBirdFly:(id)sender
{
}

Related

NSWindow update content when show/hide

I have added NSWindow element in my.xib file and inserted some element to it, such as imageView. And then created class of type NSWindow named customNSWindow and assign these class to the xib element which I have created(NSWindow). Now from another WindowController I need to show/hide the customNSWindow. This is done by putting an outlet to the WindowController.
viewController.h
#property (strong) IBOutlet NSWindow *ImageEditWindow;//(custom window)
viewController.mm
-(IBAction)ButtonClick:(id)sender {
if(! [_ImageEditWindow isVisible] ){
[_ImageEditWindow makeKeyAndOrderFront:sender];
}
}
But I don't know I how to update image in ImageEditWindow, I cannot find way to call a method inside the custom class I created, using _ImageEditWindow outlet.
Edit
Here is the custom class for NSWindow
CustomIKImageEditor.h
#interface CustomIKImageEditor : NSWindow
#property (weak) IBOutlet IKImageView *IKImg;
-(void) updateIKImage: (NSImage*)staticImageToEdit;
#end
CustomIKImageEditor.mm
-(void) updateIKImage: (NSImage*)staticImageToEdit {
NSDictionary* _imageProperties;
CGImageRef source = [self CGImageCreateWithNSImage: staticImageToEdit];
_imageProperties = NULL;
[_IKImg setImage: source imageProperties: NULL];
}
This line:
#property (strong) IBOutlet NSWindow *ImageEditWindow;//(custom window)
Should be:
#property (strong) IBOutlet CustomIKImageEditor *ImageEditWindow;//(custom window)

Objective-c - control outlet from other class

I just like to play with coding for a hobby, so probably a noob question;
I have a simple storyboard for MacOS with 2 views. Both have there own classes (main class and subclass). How can I control a outlet in the subclass from the main class?
for example
I have a button (IBAction) in the mainclass and a textfield (IBOutlet) in the subclass. I want to set the stringvalue for the textfield with a click on the button in main.
I have searched a lot last days but just don't get it. (or just need a push in the right direction)
EDIT after JingJingTao's answer:
I used the control-drag function to open the second window.
I tried the code JingJingTao gives, but the textfield doesn't respond to the action.
My classes look like this now:
ViewController.h
#import <Cocoa/Cocoa.h>
#interface ViewController : NSViewController
- (IBAction)newText:(id)sender;
#end
ViewController.m
#import "ViewController.h"
#import "ViewController2.h"
#interface ViewController ()
#property (nonatomic) ViewController2 *subclass;
#end
#implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)setRepresentedObject:(id)representedObject {
[super setRepresentedObject:representedObject];
}
- (void)newText:(id)sender {
self.subclass.textField.stringValue = #"button pressed";
}
#end
ViewController2.h
#import "ViewController.h"
#interface ViewController2 : ViewController
#property (nonatomic) IBOutlet NSTextField *textField;
#end
ViewController2.m
#import "ViewController2.h"
#interface ViewController2 ()
#end
#implementation ViewController2
- (void)viewDidLoad {
[super viewDidLoad];
}
#end
Update:
I've attached two screenshots of what it looks like in the storyboard for the first suggestion,
1) Add a view to your ViewController, set the class at the top right to 'YourView', 'YourView' is a just an NSView, add a textfield to it and hook it up.
2) Add YourView as a property to your ViewController, i.e. #property (nonatomic) IBOutlet NSView *yourView; and hook it up.
Let me know if there are any issues.
You just need to put the textfield in the public interface of your subclass, so you can access it in your main class, although it does sound like you're using inheritance and I don't think you need to but that's another topic :D.
Example:
In MainClassViewController.m
#interface MainClassViewController ()
#propert (nonatomic) Subclass *subclass;
#end
#implementation MainClassViewController
// I guess you already add your subclass to the main viewcontroller because they display on the same screen.
- (void)yourButtonTapMethod {
self.subclass.textfield.text = #"Your value";
}
In Subclass.h
#interface Subclass : NSObject
#property (nonatomic) IBOutlet UITextfield *textfield;
I use Cocoa Touch instead of Cocoa, so maybe it's NSTextfield for you. Please let me know if this does not answer your question, good luck.

Changing views on a window with a button click

what I'm basically trying to make is a very simple program that can switch back and forth between 2 views on a single window.
When the program loads there is a window with a custom view that contains a login button. When clicked, the view changes to a second custom view that contains a label and a logout button. I have this much working.
What I can't figure out is how to get the logout button to bring me back to the first view.
Here is the code for the AppDelegate class where i have the button method to switch the view:
header:
#import <Cocoa/Cocoa.h>
#interface AppDelegate : NSObject <NSApplicationDelegate>
#property (weak) IBOutlet NSWindow *window;
#property (weak) IBOutlet NSView *loginView;
- (IBAction)loginButtonClicked:(id)sender;
#end
implementation:
#import "AppDelegate.h"
#import "myCustomView.h"
#implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
}
- (void)applicationWillTerminate:(NSNotification *)aNotification {
// Insert code here to tear down your application
}
- (IBAction)loginButtonClicked:(id)sender {
[_loginView removeFromSuperview];
myCustomView *new = [[myCustomView alloc]initWithNibName:#"myCustomView" bundle:nil];
[[[self window]contentView]addSubview:[new view]];
}
#end
This is the code for my custom class that is a subclass of NSViewController.
header:
#import <Cocoa/Cocoa.h>
#class AppDelegate;
#interface myCustomView : NSViewController
#property (strong) IBOutlet NSView *logoutView;
- (IBAction)logoutButtonClicked:(id)sender;
#end
implementation:
#import "myCustomView.h"
#implementation myCustomView
- (void)viewDidLoad {
[super viewDidLoad];
// Do view setup here.
}
- (IBAction)logoutButtonClicked:(id)sender {
[_logoutView removeFromSuperview];
myCustomView *newController = [[myCustomView alloc]initWithNibName:#"MainMenu" bundle:nil];
[[[self window]contentView]addSubView:[newController view]];
//this does not work. No visible #interface for 'myCustomClass' declares the selector 'window'
}
#end
The button method to go back to the login page is where I'm stuck. Even though I've added the header file for AppDelegate into myCustomClass I cannot use the instance of NSWindow. What am I doing wrong here? Am I at least on the right track? any help here is greatly appreciated.
I also tried using #class instead of #import, but still can't use the instance of NSWindow from AppDelegate.
Here are the pictures of my two xib files:
[][1
UPDATE: The suggestions from Paul Patterson in his comments were very helpful, but haven't solved my problem. For now what I am doing to get my project to work is putting the buttons in the window instead of the views and then hiding them when i don't need them. This works and I can switch back and forth, however I still can't figure out how to use a button on a custom view itself to load a different view onto the same window.

Change NSTextView content outside an IBAction Method

I'm trying to modify the string contents of an NSTextView object. So far I've been able to make it work with the following code.
.h file
#import "AppDelegate.h"
#interface InterfaceManager : AppDelegate {
IBOutlet NSTextView *content;
}
-(IBAction)displayOutput:(id)sender;
#property (assign) IBOutlet NSWindow *window;
#end
.m file
#import "InterfaceManager.h"
#implementation InterfaceManager
-(IBAction)displayOutput:(id)sender {
[content setString:#"This is a string"];
}
#end
which all works perfectly fine and does exactly what I want.
However I need to be able to do the same thing in a void method. Here's what I've tried.
.h file
#import "AppDelegate.h"
#interface InterfaceManager : AppDelegate {
IBOutlet NSTextView *content;
}
-(void)displayOutput:(NSString *)string;
#property (assign) IBOutlet NSWindow *window;
#end
.m file
#import "InterfaceManager.h"
#implementation InterfaceManager
-(void)displayOutput:(NSString *)string {
[content setString:string];
}
#end
and it doesn't seem to work.
What am I doing wrong and how to I fix it to make it work?
Thanks in advance! :)
If I understand you correctly, you set the text when a button is pushed, but you also want to set the text without the user pushing a button. If that it the case then you don’t need a "void method", you can use any method to do it. For example, I set the client name in my options screen when I first enter the screen using this code.
- (void)viewDidLoad {
// All games allow a client and rewards
if (self.currentClient) {
self.clientInput.text = self.currentClient;
}
}
If you want to have a separate method to set the string, I think you can use something like this:
-(void)myContentSettingMethod {
[self.content setString:#"This is a string"];
}
and then call it whenever you want to set the content.

How to use an IBAction button outside it's scope

I have three IBActions, each linked with it's own button like so:
#import "Controller.h"
#import "Application.h"
#implementation Controller
- (IBAction)deal:(id)sender {
NSButton *deal = (NSButton *)sender;
...
}
- (IBAction)hit:(id)sender {
NSButton *hit = (NSButton *)sender;
...
}
- (IBAction)stay:(id)sender {
NSButton *stay = (NSButton *)sender;
...
}
#end
How do you use/call on a button outside it's scope? I'm looking to do something like this:
- (IBAction)hit:(id)sender {
NSButton *hit = (NSButton *)sender;
...
[hit setEnabled: NO];
[stay setEnabled: NO]; // Using/altering "Stay's" button
}
Thanks in advance!
IBAction is a typedef of void, used to flag up methods that you want Interface Builder (or Xcode 4's interface designer to spot. They're just ordinary methods with no outward link to anything.
What you probably want is some IBOutlets to connect out to the button. E.g. to connect to the hit button, you'd add this to your #interface:
IBOutlet NSButton *hit;
Then in Interface Builder or Xcode you should be able to control-drag a link from your class out to the button and connect the outlet — the opposite of what you've been doing to connect the button events to your class, effectively.
Because hit is an instance variable, you can access it from any method in your class.
You can declare a NSButton property as IBOutlet in your view controller and then in the Interface Builder you can link your button with it.
Something like:
#property (nonatomic, assign) IBOutlet NSButton * button;