I use following code in VC1 :
ResultViewController *primaryView = [[ResultViewController alloc] initWithNibName:#"ResultView" bundle:nil];
UIImageView *profileView = [[UIImageView alloc] initWithImage:[UIImage imageNamed: #"profile.png"]];
[_coinView setPrimaryView: primaryView.view];
[_coinView setSecondaryView: profileView];
[_coinView setSpinTime:1.0];
than I have following code in a View file:
-(IBAction) flipTouched:(id)sender{
[UIView transitionFromView:(displayingPrimary ? self.primaryView : self.secondaryView)
toView:(displayingPrimary ? self.secondaryView : self.primaryView)
duration: _spinTime
options: UIViewAnimationOptionTransitionFlipFromLeft+UIViewAnimationOptionCurveEaseInOut
completion:^(BOOL finished) {
if (finished) {
displayingPrimary = !displayingPrimary;
}
}
}];
}
this code combo helps me to flip a view shown in VC1, it works well, but now what I am trying to do is to have a button shown in VC1, when it clicked, I can also flip the same view. So I guess the functional code should be written in VC1, and I just cannot figure out a good code to do that, anyone can help me please? BTW, how can I call "[self loadData]" of VC1 in view file?
It sounds like you just need to wire up flipTouched: to a UIButton:
UIButton *myButton = [[UIButton alloc] init]; // Or an outlet from your XIB/storyboard
[myButton addTarget:self
action:#selector(flipTouched:)
forControlEvents:UIControlEventTouchUpInside];
[_coinView flipTouched: nil ];
I find out this works good for me.
Related
I have a custom UIImagePickerController that works nicely, only I am facing one issue that I feel should be fairly simple - I just have yet to figure out the solution.
Upon touching my custom added "photo" button, I have it targeted to the build in takePicture method of the UIIPC. Here is my code
#interface CustomCameraController ()
#end
#implementation CustomCameraController {
CGFloat width, height;
}
- (instancetype)init {
if (self = [super init]) {
width = self.view.frame.size.width, height = self.view.frame.size.height;
self.allowsEditing = YES;
self.sourceType = UIImagePickerControllerSourceTypeCamera;
self.showsCameraControls = NO;
self.toolbarHidden = YES;
[self buildCameraOverlay];
}
return self;
}
- (void)buildCameraOverlay {
UIView *customOverlay = [UIView alloc] ...
// ... Custom overlay setup done here
_takePhoto = [[CustomButton alloc] initWithFrame:CGRectMake(0, 0, heightBottomBar*.5, heightBottomBar*.5)];
_takePhoto.center = CGPointMake(bottomBar.frame.size.width/2, bottomBar.frame.size.height/2);
[_takePhoto setImage:[UIImage imageNamed:#"camera button icon"] forState:UIControlStateNormal];
[_takePhoto addTarget:self action:#selector(takePicture) forControlEvents:UIControlEventTouchUpInside];
[bottomBar addSubview:_takePhoto];
// ...
self.cameraOverlayView = customOverlay;
}
This is done in my custom controller CustomCameraController init call.
The problem is, upon taking the picture via takePicture, the camera shutter goes off, everything works just fine, but the controller dismisses itself. I'm trying to figure out how to stop it from closing immediately after taking the picture, so I can A)present the taken picture, and B) give the user the option to choose the image or cancel and retake another one (returning to the camera)
If anyone knows why this happens or something that I am missing / doing incorrectly please let me know. I'm sure it's a simple answer - just can't seem to figure it out. Thanks!
The most common reason for such a weird behaviour is usually lack of delegate methods (for UIImagePickerController in this case) or their wrong implementation.
I have an uiImageview which is intended to use in multiple viewControllers.
What I am thinking is an independant UiImageview that can bind and
unbind from viewController to viewController if needed.
Here is my bind/ unbind part of code.
#synthesize uiImgView1;
-(void) sendUIObjToViewController : (UIImageView*)imgView : (UIViewController*)vController{
[vController.view addSubview:imgView];
}
-(UIImageView*)constructUiImgView : (NSString*) imgAddress{
UIImageView *imgView = [[UIImageView alloc] initWithFrame : CGRectMake(50,50,100,100)];
[imgView setImage:[UIImage imageNamed:imgAddress]];
return imgView;
}
-(void) construct : (UIViewController*) vController{
uiImgView1 = [self constructImgView : #"abc.png"];
[self sendUIObjToViewController : uiImgView1 : vController];
}
If I call addSubView method from scope of constructUiImgView,
addSubView works.
-(UIImageView*)constructUiImgView : (NSString*) imgAddress : (viewController*)vController{
UIImageView *imgView = [[UIImageView alloc] initWithFrame : CGRectMake(50,50,100,100)];
[imgView setImage:[UIImage imageNamed:imgAddress]];
[vController.view addSubView imgView];
return imgView;
}
However, that means constructed uiImageView is depandant to one
specific viewController and I don't want to do that.
and If I call sendUiObjToViewController method, uiImageView
should bind to view of viewController but not works.
Am I doing some mistake of referencing variable or something??
My knowledge of Object-c is very limited so I really
need you guys help. Could you tell me what is wrong with my code?
any help will be appreciated.
I started writing a project without really using the storyboard (as i was following a tutorial about creating an options menu for a game).
So i am trying to retroactively implement a way of moving between 2 UIViewControllers (MainGame and MatchScreen) that each has a UIView (MainMenu and VSComputer respectively).
Now in the MainMenu UIView I have created 4 buttons, with all their positions and settings, two of which are subviews - options and stats - that so appear and disappear when clicked on the current ViewController.
Now, the problem is, I need to somehow move from the main menu to the match screen using one of the other buttons named 'matchScreen'. I feel there must be something that i'm missing as no answer found in my research has worked for me yet.
I have embedded my main UIViewController with a navController (as suggested in other questions that i've seen) and then added a push segue to the 2nd ViewController. But i don't know how to make this apparent to my code. My code recognises nothing that i do in the storyboard it seems.
#interface JDTHMainMenu(){
JDTHOptionsScreen *theOptionScreen;
JDTHPlayerStats *thePlayerStatsScreen;
UIButton *optionScreenButton;
UIButton *matchScreenButton;
UIButton *deckScreenButton;
UIButton *statsScreenButton;
bool isPhone;
bool isOptionScreenOpen;
bool isPlayerStatsScreenOpen;
bool matchScreenButtonPressed;
UIView *tint;
int screenWidth;
int screenHeight;
AppData *appData;
}
So to be clear, JDTHMainMenu is a UIView in the JDTHViewController - the original, JDTHMatchScreen is the second ViewController and there is a UIView called JDTHVersusScreen. Im trying to go from one to the other. Also the JDTHAppDelegate only has return YES in the didFinishLaunching method. I feel i'm getting closer....but perhaps not...
-(id)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
appData = [AppData sharedData];
NSLog(#"Main Menu has loaded");
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
isPhone = YES;
} else {
isPhone = NO;
}
screenWidth = self.frame.size.width;
screenHeight = self.frame.size.height;
isOptionScreenOpen = NO;
isPlayerStatsScreenOpen = NO;
matchScreenButtonPressed = NO;
[self showOptionScreenButton];
[self showPlayerStatsScreenButton];
[self matchViewControllerButton:matchScreenButton];
[self deckViewerControllerButton:deckScreenButton];
}
return self;
}
-(IBAction)matchViewControllerButton:(UIButton *)sender{
CGRect rect;
UIFont *theFont;
if (isPhone == YES) {
rect = CGRectMake(38, 150, 134, 116);
theFont = [UIFont fontWithName:#"Zapfino" size:22];
} else {
rect = CGRectMake(275, 600, 200, 150);
theFont = [UIFont fontWithName:#"Zapfino" size:28];
}
matchScreenButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[matchScreenButton addTarget:self action:#selector(goToMatchView:sender:) forControlEvents:UIControlEventTouchDown];
[matchScreenButton.titleLabel setFont:theFont];
[matchScreenButton setTitle:#"VS Comp" forState:UIControlStateNormal];
matchScreenButton.frame = rect;
[matchScreenButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
[matchScreenButton setBackgroundImage:[UIImage imageNamed:#"MainMenuOptionButton"] forState:UIControlStateNormal];
[self addSubview:matchScreenButton];
}
-(void)goToMatchView:(UIStoryboardSegue *)segue sender:(id)sender{
if ([segue.identifier isEqualToString:#"pushToMatchView"]) {
JDTHMatchScreen *vc2 = (JDTHMatchScreen *)segue.destinationViewController;
vc2.name = self.textField.text;
}
}
A storyboard app has an entry in the info.plist file that tells the app that it's a storyboard app, and what the name of the storyboard is ("Main storyboard file base name" is the name of the key, and the value is the file name, without extension, of your storyboard). You would need to add this to your project to get it to recognize the storyboard. Also, non-storyboard apps usually have code in the app delegate to set up the initial controllers. This should be deleted for a storyboard app (the only code in the default template in didFinishLaunchingWithOptions: is "return YES"). It might be easier to just start a new storyboard based single view project and copy your files to it.
I have a UIViewController named MainViewController. I have an another class named ViewMaker.
In the ViewMaker class I have a function as follows.
-(UIView *)GetContentView
{
UIView *return_view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)];
UIButton *done_button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
done_button.frame = CGRectMake(300, 300, 100, 50);
[done_button setTitle:#"Done" forState:UIControlStateNormal];
[return_view addSubview:done_button];
return return_view;
}
In the above function actually I will add more views like textviews, labels etc.
In the MainViewController class I am calling the above method as follows.
-(void)CreateViews
{
UIView *content_view = [[[ViewMaker alloc] init] GetContentView];
[self.view addSubview:content_view];
}
Now I have added a view with done button(and other components like textviews, labels etc) from the MainViewController class.
My problem is about adding a target function for the done button. When I click the done button I want to perform some actions(like add some views) in the MainViewController according to the datas in the view that was returned from the ViewMaker class.
How can I do this?
Thanks in advance.
I assume your ViewMaker class is same as PopupContentMaker,
add this to your done button:
[done_button addTarget:self action:#selector(doSomething) forControlEvents:UIControlEventTouchUpInside];
in PopupContenMaker:
- (void)doSomething{
if ([self.delegate respondsToSelector:#selector(handleDoneButtonTap)]) {
[self.delegate performSelector:#selector(handleDoneButtonTap) withObject:self];
}
}
declare a delegate property in your PopupContentMaker, and make MainViewController the delegate,
-(void)GetContentView
{
PopContentMaker *pcm = [[PopupContentMaker alloc] init];
pcm.delegate = self;
[self.view addSubview:[pcm GetContentView]];
}
-(void)handleDoneButtonTap{
//Do action after done button
}
You can try:
[done_button addTarget: self
action: #selector(buttonClickedMethod)
forControlEvents: UIControlEventTouchUpInside];
In this way you can call a custom method when button is pressed.
You can pass the target and action as parameters to the getContentView method or you can use the delegate pattern.
I've just started learning the basics of Xcode and Objective C and I am making a basic torch app as a starting point.
I've already picked up most of the basics of things, but I don't know how to make a reference to another element from an separate action (I have no idea if this terminology is correct).
For example:
- (IBAction)screenButtonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
if (_ScreenOnOff) {
[self.view setBackgroundColor:[UIColor blackColor]];
[button setTitle:#"Screen (Off)" forState: UIControlStateNormal];
}
else {
[self.view setBackgroundColor:[UIColor whiteColor]];
[button setTitle:#"Screen (On)" forState: UIControlStateNormal];
}
_ScreenOnOff = !_ScreenOnOff;
}
I have a button on the storyboard which is linked to that, and I have a UIImageView which I want to show and hide (depending on the if's).
I've looked everywhere about how to do this and put it as many ways as I can into Google, but no luck.
This might be a baby step in Objective C, but please help as it will teach me.
Thanks in advance.
Declare an IBOutlet for the UIImageView in the header file:
#property (weak) IBOutlet UIImageView *myImageView;
After that, connect the UIImageView to this IBOutlet in Interface Builder.
Then, you can reference (and hide it) like:
self.myImageView.hidden = _ScreenOnOff;
in your implementation file.
See also: Creating and Connecting an Outlet
This action is probably owned by your view controller. If your view controller has a UIImageView property named 'imageView' you can access it from inside your action just like you're doing with the _ScreenOnOff.
To hide your image view you could do something like this:
self.imageView.hidden = YES;
Note that it is important to use self.imageView and self.ScreenOnOff instead of accessing directly the property by _imageView. When you declare a property in your class (be it an IBOutlet or not) the compiler synthesizes accessor methods to that property (get and sets). So when you call self.ScreenOnOff it would be the similar of doing [self ScreenOnOff].
The only places where you will access the property directly by '_' is inside init and dealloc methods. '- (void) viewDidLoad' is on type of init method. I guess you want something like this:
- (IBAction)screenButtonClicked:(id)sender {
UIButton *button = (UIButton *)sender;
if (self.ScreenOnOff) {
[self.view setBackgroundColor:[UIColor blackColor]];
[button setTitle:#"Screen (Off)" forState: UIControlStateNormal];
self.imageView.hidden = YES;
}
else {
[self.view setBackgroundColor:[UIColor whiteColor]];
[button setTitle:#"Screen (On)" forState: UIControlStateNormal];
self.imageView.hidden = NO;
}
self.ScreenOnOff = !self.ScreenOnOff;
}
I hope that helps.