hiding and displaying uitoolbar on same button click - iphone

I am trying to hide and display a UIView on BarButtonItem click. Priviously i also posted the question regarding same but didnt find any suitable answer. I created UIView manually in IB and just placed it in view so it must be shown as soon as view is loaded but i made it hidden in viewDidLoad method by writing
myvew.hidden = YES;
secondly, when i click BarButtonItem then i set
-(IBAction)mymethod
{
myview.hidden = NO;
}
so its diplaying view but when i again click on it it must hide.. how do i do it?

Put the following statement in your button action
myview.hidden = !myview.hidden ;
So your code must be like below.
-(IBAction)mymethod
{
myview.hidden = !myview.hidden ;
}

if (myview.hidden == YES)
{
myview.hidden = NO;
}
else
{
myview.hidden = YES;
}
Check if the view is already hidden and then show, and if not hidden then hide it.

You should do in this way
-(IBAction)mymethod
{
if( myview.hidden == NO ) myview.hidden = YES;
else myview.hidden = NO;
}

Related

Add Power Button to Calculator App

I am trying to create a "Power Button" for my calculator app that will turn the calculator on/off. I created the button:
-(IBAction) *Power;
Without the power button, the app starts with "0" in the LCD display, and my buttons are all working; but how can I tell my app that the LCD should display "" and the buttons cannot be manipulated until the power button is pressed? Would a simple if-then function work? Can I put -(IBAction) buttons within if-thens?
Create a BOOL property, that and check for YES in all the IBACtions.
You need to set it to YES/NO on the method Power, typically as:
Initiaze it with NO in your init or viewDidLoad, whichever is applicable.
-(IBAction) power{
self.powerOn=!self.powerOn;
}
-(IBAction) otherMethod{
if(self.powerOn){
//do your stuff
}
}
Also, method with pointer -(IBAction) *Power; is not that you need here.
1) declare a variable to track power on/off
#property(nonatomic, assign) BOOL powerOn;
in viewDidLoad,
assign _powerOn = NO; or _powerOn = YES;
2) inside your power button pressed event,
-(IBAction) Power;{
//if on, then off
if(self.powerOn){
//make display ""
self.powerOn = NO;
}else{ //if off, the on
self.powerOn = YES;
//make display "0"
}
}
3) add following line as the 1st line in all other button click events
-(IBAction) otherMethod{
if(!self.powerOn) return;
//your code
}
You Need to create IBOutlets for your Buttons, to edit behaviors of them while running the app.
These IBOutlets are created like this:
IBOutlet UIButton *myPowerButton;
You need to link them with the button in Interface Builder where you created the Outlet for.
There you can specify the 'Enabled' behavior to be YES or NO, so you can make a Power Button to set if the user can work or not.
For further information please read Apples Documentation about IBOutlets and Buttons.
UIButton Apple Documentation
I would do this way, assuming the title of the button is Power:
-(IBAction) Power
{
if(self.powerOn)
{
//make display ""
self.powerOn = NO;
for (UIView* subView in self.view.subviews)
{
if ([subView isMemberOfClass:[UIButton class]] && subView.titleLabel.text != #"Power")
subView.userInteractionEnabled = NO;
}
}
else
{ //if off, the on
self.powerOn = YES;
//make display "0"
}
}
I would disable the User interaction of all other button when powerbutton is off.

UIActivityViewController not dismissing

I'm trying to add a custom UIActivity to a UIActivityController. When I click on the item, it presents the view controller I want it to, but when I finish with that view controller, the original UIActivityViewController is still there. My question is, how and where do I dismiss the activity view controller? This is the code in my custom UIActivity.
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems{
self.activityTitle = #"Text Editor";
self.activityType = #"TextEdit";
self.activityImage = [UIImage imageNamed:#"Icon.png"];
if (activityItems) return YES;
else return NO;
}
- (void)prepareWithActivityItems:(NSArray *)activityItems{
for (NSString *path in activityItems) {
if ([path lastPathComponent]) {
self.file = path;
}
}
}
- (UIViewController *)activityViewController{
ACTextEditController *actec = [[ACTextEditController alloc] initWithFile:_file];
return actec;
}
EDIT
I've tried doing this, and I know it is called because I tried logging something in it and it was called
- (void)activityDidFinish:(BOOL)completed{
if (completed) {
[self.activityViewController dismissViewControllerAnimated:YES completion:nil];
}
}
however, it's still there when the view controller dismisses. Why?
Here's a working example:
http://www.apeth.com/iOSBook/ch26.html#_activity_view
Notice that we end up by calling activityDidFinish:. That is what tears down the original interface. It may be that that is the step you are omitting. If not, just compare what you're doing with what I'm doing, since what I'm doing works (on iPhone).

how to check that any of the button is clicked or not in iphone application

I have six button i want that user must select any of the six button before moving to next screen if it does not select any then it should not move to next screen I have read to use BOOL flag but this show which button is clicked i want that if any of the six buttons is clicked then user can move to next screen
-(IBAction)locationOneButtonAction{
// your stuff
_flag = YES;
}
-(IBAction)locationTwoButtonAction{
// your stuff
_flag = YES;
}
I think the best solution this problem is to declare a bool for your class (let's say it is called "ClickViewController" like (so at the top)
#interface ClickViewController () {
bool wasClicked;
}
...
-(IBAction)locationOneButtonAction{
// your stuff
wasClicked = YES;
}
Then when the button that should move to the next page is clicked utilize a method like this.
-(void)nextPageClicked{
if (wasClicked){
[self performSegueWithIdentifier:#"segueIdentifier" sender: self];
} else {
// do something else here, like tell the user why you didn't move them
}
}
This means that you cannot draw the segue from the button to the next view. If you are using storyboards, this means that you can't draw the segue from the button that is supposed to move it to the next view, rather you should draw it from the view controller icon to the next view. Then select the segue, name it, and use the method above with the name.
I'm also not sure if you mean that you want each of the buttons to move to the next view, if that is the case, you can use basically the same code, but just put the code [self performSegueWithIdentifier:#"segueIdentifier" sender: self]; line in action for the button.
Hopefully this helps.
Intially add BOOL _flag; int curIndex; in .h file.
Now add intially _flag = FALSE; in viewDidLoad method as it indicate no button clicked.
Make userInteractionEnabled for each button to be able to click and add tag to each button.
Now in action method of each click of button do this:
(IBAction)locationOneButtonAction:(id)sender{
curIndex = [sender tag]; //which button clicked
// your stuff
_flag = TRUE;
}
-(IBAction)locationTwoButtonAction{
curIndex = [sender tag]; //which button clicked
// your stuff
_flag = YES;
}
........
........
Now check like this if button clicked:
if(_flag)
{
NSLog(#"Button clicked with tag: %d",curIndex);
}
else
{
NSLog(#"Not any Button clicked");
}

i have a problem in UIImageView's animating

-(IBAction) change {
self.imageView.animationImages = myImages;
self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}
hello, i'm studying iOS programming.
but i have a question.
i have a button and when i click the button, then this method will be called.
first i click the button, then this code will start the if statement. that's what i want.
i click the button again, i think that will execute the else statement.
but it always execute the if statement only.
why is that?
i really don't know why is that. please help me
I think setting the properties like animationImages or animationDuration will stop the animation, so that by clicking, you every time stop and then just after (re)start it in the if part. Try setting these two properties outside the action method you wrote, and just let the if/else sequence.
-(IBAction) change {
// set these two anywhere else
//self.imageView.animationImages = myImages;
//self.imageView.animationDuration = 2;
if(self.imageView.isAnimating == NO){
[self.imageView startAnimating];
NSLog(#"if bool = %d", self.imageView.isAnimating);
}
else {
self.imageView stopAnimating];
NSLog(#"else bool = %d", self.imageView.isAnimating);
}
}

iOS segmented control problem

So I have a strange problem with my segmented control I am trying to use. Essentially I have a preferences panel that displays via a popover when a button is pushed.
The problem: I am trying to save the state so when the view loads, the segmented control should save it's selected item. Here is what I am doing so far...
-(void)viewWillAppear:(BOOL)animated {
if(!self.mainViewController.isThreaded){
self.threadedView.selectedSegmentIndex == 0;
//[self.threadedView setSelectedSegmentIndex:0];
//I can't do this because if I do it, it rexecutes the changeSegment method,
// which I do not want
}
if(self.mainViewController.isThreaded){
self.threadedView.selectedSegmentIndex == 1;
//[self.threadedView setSelectedSegmentIndex:1];
}
//threadedView.momentary = NO;
}
-(void)changeSegment {
if(self.threadedView.selectedSegmentIndex == 0){
self.mainViewController.isThreaded = NO;
[self.threadedView setSelectedSegmentIndex:0];
}
if(self.threadedView.selectedSegmentIndex == 1){
self.mainViewController.isThreaded = YES;
[self.threadedView setSelectedSegmentIndex:1];
}
}
now the problem is, when the popover appears, it does not load the state to the segmented control, as I understand it should. Can anyone point out what I may be doing wrong? Thanks
In viewWillAppear, if you want to set them and not test them it should be:
self.threadedView.selectedSegmentIndex = 0/1;
not
self.threadedView.selectedSegmentIndex == 0/1;, unless I'm missing something.